Say I have a folder called
Foo located in
/home/user also being represented by
I want to have a variable
a="~/Foo" and then do
-bash: cd: ~/Foo: No such file or directory
However if I just do
cd ~/Foo it works fine. Any clue on how to get this to work?
You can do (without quotes during variable assignment):
But in this case the variable
$a will not store
~/Foo but the expanded form
/home/user/Foo. Or you could use
eval cd "$a"
You can use
$HOME instead of the tilde (the tilde is expanded by the shell to the contents of
Although this question is merely asking for a workaround, this is listed as the duplicate of many questions that are asking why this happens, so I think it’s worth giving an explanation. According to https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06:
The order of word expansion shall be as follows:
Tilde expansion, parameter expansion, command substitution, and arithmetic expansion shall be performed, beginning to end.
When the shell evaluates the string
cd $a, it first performs tilde expansion (which is a no-op, since
$a does not contain a tilde), then it expands
$a to the string
~/Foo, which is the string that is finally passed as the argument to
A much more robust solution would be to use something like sed or even better, bash parameter expansion:
or if you must use sed,
cd $(echo "$somedir" | sed "s#^~#$HOME#")
If you use double quotes the ~ will be kept as that character in $a.
cd $a will not expand the ~ since variable values are not expanded by the shell.
The solution is:
eval “cd $a”