Search code examples
bashcd

Change directory to a path specified in a file with tilde


I have a file, say: ~/cwd. The content of this file is a single line:

~/tmp

I want fo cd to this (~/tmp) dir. I'm trying:

> cd `cat ~/cwd`

And got:

-bash: cd: ~/tmp: No such file or directory

Why the RELATIVE paths failed? When the content of the ~/cwd is absolute path - it works.


Solution

  • Try this:

    eval cd `cat ~/cwd`
    

    The '~' needs to be expanded by the shell. The eval causes the command to be run through the shell's command processing, which includes '~' expansion.