Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tilde in path doesn't expand to home directory

Tags:

Say I have a folder called Foo located in /home/user/ (my /home/user also being represented by ~).

I want to have a variable

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?

like image 289
Benjamin Avatar asked Apr 21 '11 18:04

Benjamin


2 Answers

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"
like image 61
bmk Avatar answered Nov 10 '22 03:11

bmk


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";
like image 24
user268396 Avatar answered Nov 10 '22 05:11

user268396