Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.9k views
in Technique[技术] by (71.8m points)

Bash evaluation changed in version 4.4?

I have a bash function which is used to shorten directory names, one way is to shorten "/home/USER" to "~". With bash version 4.2 it produces the expected result, but when version 4.4 the result is expanded to "/home/USER".

#!/usr/bin/env bash
function convertDirRelative()
{
    local retVar="$1" dir
    shift

    dir="$*"
    # [...]
    dir="${dir//${HOME}/~}"

    eval "$retVar="$dir""
}
output=""
convertDirRelative output $HOME/test
echo "${BASH_VERSION}: Output: $output"

When executed with bash 4.2 this is the result, which is expected:

4.2.46(2)-release: Output: ~/test

When executed with bash 4.4 this is the result:

4.4.19(1)-release: Output: /home/USER/test

How can I write the function so that it produces the expected result in both bash 4.2 and 4.4?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This line seems to work in both versions:

dir=${dir//${HOME}/~}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...