Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.4k 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 Reply

0 votes
by (71.8m points)

This line seems to work in both versions:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...