Say you have directory ~/mytool that softlinks to /usr/local/mytool. Then cd mytool leaves your current directory as ~/mytool, which may cause scripts to behave incorrectly. Is there a way to avoid this behaviour?

With some googling, I see that you can achieve this as follows:

cd $1 ; cd `pwd -P`

Is there no switch to 'cd'? Environment variables?

link|improve this question

44% accept rate
feedback

3 Answers

up vote 7 down vote accepted

If you type set -P in bash all commands such as cd, pwd will follow the physical path. Else you can use cd -P and pwd -P for temporary changes to the default behavior.

From the manpage of bash:

          -P      If  set,  the shell does not follow symbolic links when executing commands such as cd that change the cur-
                  rent working directory.  It uses the physical directory structure instead.  By default, bash  follows  the
                  logical chain of directories when performing commands which change the current directory.

To make this permanent, put it in your ~/.bashrc file, for example.

link|improve this answer
feedback

"cd" is a built in in most shells. In bash, you can get the behavior you want by adding

set -P

in a startup script (.bashrc, say).

link|improve this answer
feedback

On Ubuntu/Debian (not sure about BSD), cd -P symlink puts me in the resolved symlink path. (Same behavior as pwd -P)

Tested using:

mkdir a
ln -s a b
cd -P b && pwd
link|improve this answer
Yep that seems to work on RHEL (but not on MacOSX, which I was using before) – Steve Bennett Jan 25 at 3:31
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.