I want to store a path I often use as an environment variable, then call this variable to construct a path name.
$ mypath="~/local/bin/"
$ newFile="${mypath}newFile.sh"
$ echo $newFile
~/local/bin/newFile.sh
That appears to work.
But, if I call $newFile as the input to touch, it fails:
$ touch $newFile
touch: ~/local/bin/newFile.sh: No such file or directory
$ touch "${mypath}newFile.sh"
touch: ~/local/bin/newFile.sh: No such file or directory
Both don't work. What am I missing?
P.S. I'm certain that this has been discussed already but I'm new to bash so I don't know what the jargon is to search yet. Thanks for any help.
newFile
(literal name). You probably meanttouch $newFile
. That would break as soon as$mypath
contains a whitespace character. The real issue is the expansion of the tilde, which does not happen in quotes. Use$HOME
instead. – Kusalananda May 23 '19 at 16:34