This question already has an answer here:
I have made a directory ~/test_myDir
I then run the following bash script:
x="myDir"
dirName="~/test_$x"
cd $dirName
echo "hey" > test.txt
I get the following error:
test.sh: line 5: cd: ~/test_myDir: No such file or directory
I then remove the quotes from the second assignment:
x="myDir"
dirName=~/test_$x
cd $dirName
echo "hey" > test.txt
The script runs without error.
What is going on here? I ran into this issue in a larger, more complicated script, and I narrowed it down to my use of quotes in a variable assignment that contained another variable.
Still, from the error message, it looks like the full path is being expanded correctly in the "cd" call.
Quotation marks prevent expansion of
~
. Replace~
with$HOME
or usedirName=~/"test_$x"
.From the manual's explanation of tilde expansion: