Getting a negative result when testing HOME directories with tilde extension ~

I get a strange error, I can’t understand why.

Especially because it works in one place, but not in another scenario.

I have his code that checks if these directories exist - and the HOME dirs with ~ does not work in this piece of code:

if [[ ! -d "$valueToTest" ]]; then
    echo 'Failed>>:'$valueToTest;
fi;

I get the output:

Failed>>:~/.workspace/
Failed>>:~/.ssh
Failed>>:~/.workspace/vim/temp
Failed>>:~/.workspace/vim/backup
Failed>>:~/.workspace/vim
Failed>>:~/test

As I said, this happens ONLY with the extension ~ to represent my HOME directory.

And this is ONLY the place where this happens, since I have OTHER code that checks the path with ~ ie my HOME directory with ~ extension, And they work great!

Directories are read from the value of part of the associative array, but I do not understand why this would be a problem. You can also see that there are no quotes or related links on the output.

- .

:

$valueToTest
${valueToTest}
"${valueToTest}"

.

0
1

Tilde , ~ - . , ~ , .

$valueToTest - ~/.workspace/, :

[[ ! -d "$valueToTest" ]]
[[ ! -d $valueToTest ]]
[ ! -d $valueToTest ]

:

test ! -d '~/.workspace/'

:

test ! -d ~/.workspace/

test ! -d /home/user/.workspace/

, , , :

valueToTest=~/.workspace/    ## Correct assignment.
valueToTest=~'/.workspace/'  ## Correct assignment.
valueToTest=~"/.workspace/"  ## Correct assignment.

valueToTest='~/.workspace/'  ## Incorrect assignment.
valueToTest="~/.workspace/"  ## Incorrect assignment.

set -- ~/.workspace/         ## Correct assignment.
valueToTest=$1               ## (simulates bash script.sh ~/.workspace/)

set -- "~/.workspace/"       ## Incorrect assignment.
valueToTest=$1

The Hack

, printf %q eval, . :

printf -v temp %q "$valueToTest"
eval "valueToTest=$temp"

$valueToTest , .

Update

Bash 4.3, printf %q - (~). , :

printf -v temp %q "$valueToTest"
temp=${temp//\\\~/\~}
eval "valueToTest=$temp"

$HOME :

printf -v temp %q "$valueToTest"
temp=${temp//\\\~/\~}
temp=${temp//\\\$HOME/\$HOME}
eval "valueToTest=$temp"

- :

valueToTest=${valueToTest//\~/$HOME}
valueToTest=${valueToTest//\$HOME/$HOME}
0

All Articles