James Tauber

journeyman of some

blog > 2008 > 03 > 18 >

Inconsistent Symlinks

Set up directories as follows:

drwxr-xr-x 2 jtauber staff 68 Mar 18 17:57 A drwxr-xr-x 3 jtauber staff 102 Mar 18 17:58 B lrwxr-xr-x 1 jtauber staff 3 Mar 18 17:58 C -> B/C

Now cd into B

jtmbp:TEST jtauber$ cd B

If you try to "execute" A from here, it tells you it's a directory

jtmbp:B jtauber$ ../A -bash: ../A: is a directory

Now go back to the top directory

jtmbp:B jtauber$ cd ..

and cd into C (the symlinked directory)

jtmbp:TEST jtauber$ cd C

If you try to execute A from here, it can't find it via "../A" only "../../A"

jtmbp:C jtauber$ ../A -bash: ../A: No such file or directory jtmbp:C jtauber$ ../../A -bash: ../../A: is a directory

so the attempt at execution is based on the actual absolute path, not the path based on following the symlink.

However,

jtmbp:C jtauber$ cd ../A jtmbp:A jtauber$

So cd uses the path based on following the symlink but relative paths for execution do not.

I realise this is likely because cd is shell-based (and so knows the path that was followed to get to the current directory) whereas execution is a system call (which doesn't know the path that was followed to get to the current directory) but it's interesting nevertheless (and occasionally annoying).

This is on OS X but the behaviour is the same on Linux as far as I know.

Categories:
prev « os_x » next

Comments (7)

sapphirecat on March 18, 2008:

Yes, it is because cd is tracked by the shell. I find it a tad confusing, so I try to disable it. In bash, I do 'alias cd="cd -P"' (although there may be a better way), or in zsh, 'setopt chase_links'.

If the shell were to do more work, it could probably make the execution happen, but down that way lies madness. Any program that tried to find itself via argv[0] would then be pointed into the wrong directory.

hans on March 19, 2008:

The same behaviour is seen in the difference between pwd (shell builtin) and /bin/pwd

This caused me some troubles in the past.

hans on March 19, 2008:

The same behaviour is seen in the difference between pwd (shell builtin) and /bin/pwd

This caused me some troubles in the past.

hans on March 19, 2008:

The same behaviour is seen in the difference between pwd (shell builtin) and /bin/pwd

This caused me some troubles in the past.

I am not hans on March 19, 2008:

In firefox and refresh after pressing submit comment, it is getting submitted again

I am not hans on March 19, 2008:

In firefox and refresh after pressing submit comment, it is getting submitted again

Edward O'Connor on March 21, 2008:

This is one of those annoying unix niggles that Plan 9 actually addressed, by making . and .. purely textual operations on pathstrings.

(Sorry I haven't replied to your most recent email, btw; have been very swamped.)

Created: March 18, 2008
Last Modified: March 18, 2008
Author: James Tauber