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?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…