Tilde in path doesn't expand to home directory
Say I have a folder called Foo
located in /home/user/
(my /home/user
also being represented by ~
).
I want to have a varia开发者_如何学Cble
a="~/Foo"
and then do
cd $a
I get
-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):
a=~/Foo
cd "$a"
But in this case the variable $a
will not store ~/Foo
but the expanded form /home/user/Foo
. Or you could use eval
:
a="~/Foo"
eval cd "$a"
You can use $HOME
instead of the tilde (the tilde is expanded by the shell to the contents of $HOME
).
Example:
dir="$HOME/Foo";
cd "$dir";
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 cd
.
A much more robust solution would be to use something like sed or even better, bash parameter expansion:
somedir="~/Foo/test~/ing";
cd "${somedir/#\~/$HOME}"
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"
精彩评论