I am currently trying understand the behavior of Bash's cd
when it comes to symbolic links. By default, cd
should follow the logical directory structure and should not resolve symbolic links.
cd
behaves like this in my first example. However, I came up with a second example where cd
seems to follow the physical structure. Is this expected behavior? What's the difference?
Setup
$ cd
$ mkdir -p test/a/b/c test/d
$ tree test
test
├── a
│ └── b
│ └── c
└── d
4 directories, 0 files
$ cd test/d
$ ln -s ../a/b/c symlink
1st example
$ pwd
/home/hexcabron/test/d
$ tree ..
..
├── a
│ └── b
│ └── c
└── d
└── symlink -> ../a/b/c
5 directories, 0 files
$ cd symlink/../../a
$ pwd
/home/hexcabron/test/a
2nd example
$ cd ../d
$ pwd
/home/hexcabron/test/d
$ mkdir -p symlink/../e
$ tree ..
..
├── a
│ └── b
│ ├── c
│ └── e
└── d
└── symlink -> ../a/b/c
6 directories, 0 files
$ cd symlink/../e
$ pwd
/home/hexcabron/test/a/b/e
test/a/b/c
, socd symlink/../e
is the equivalent ofcd test/a/b/c/../e
, which is the equivalent ofcd test/a/b/e
. In fact, I don't even see any inconsistent behavior. In both your examples, the behavior is the same. – Tim Kennedy Mar 07 '18 at 16:44/home/hexcabron/test/d/e
and fail. – Mar 07 '18 at 16:45symlink
so it'ssymlink/
, that tells bothmkdir
andcd
to follow that path as a directory, as long as it exists, and as long as it's not a loop of symlinks that point to each other more than 20 times (MAXSYMLINKS defined in bits/param.h) This is part of the POSIX specification around pathname resolution. http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap04.html#tag_04_11 – Tim Kennedy Mar 07 '18 at 16:59cd
in the first example then also have followed the path as a directory (= physical, not logical), leading tocd /home/hexcabron/test/a/a
and fail? – Mar 07 '18 at 17:15..
. Also, I just tried your setup at home, and on my Ubuntu system, I am unable to duplicate your behavior. If Icd symlink
, pwd shows metest/d/symlink
, and cdsymlink/..
sends me back totest/d
. – Tim Kennedy Mar 07 '18 at 17:43