Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

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.

share|improve this answer

"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).

share|improve this answer

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
share|improve this answer
    
Yep that seems to work on RHEL (but not on MacOSX, which I was using before) –  Steve Bennett Jan 25 '12 at 3:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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