Premise
I've read what's listed in Bibliography regarding cd
, pwd
, set -P
.
By default, or when the
-L
option is supplied, symbolic links in directory are resolved aftercd
processes an instance of ‘..
’ in directory. (ref. Bash reference manual - sec. "4 Shell Builtin Commands" -cd
paragraph)
If ‘
..
’ appears in directory, it is processed by removing the immediately preceding pathname component, back to a slash or the beginning of directory. (ibid)
My version of Bash is
GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)
Problem
To test things out I have the following file scheme in my current working directory that is the root of my experiment (dir111
is a symbolic link to dir010
).
dir010/
dir020/
dir110/dir111 -> ../dir010
my_dir="$PWD"
cd dir110/dir111 # 1
cd .. # 2
cd dir111 # 3
cd ../dir020 # 4
cd "$my_dir" # 5
cd dir110/dir111/../dir020 # 6
cd - # 7
cd dir110/dir111/../../dir020 # 8
- sets the current working directory to
"$my_dir"/dir110/dir111
, as expected[^1]. - sets the current working directory to
"$my_dir"/dir110
, as expected[^1]. - back to 1.
- sets the current working directory to
"$my_dir"/dir020
, unexpected. I would expectcd
to fail at"$my_dir"/dir110/dir020
(see below for explanation). - back to the root of my experiment.
- same as 4.
- back to the root of my experiment.
- sets the current working directory to
"$my_dir"/dir020
, as expected[^1], but in contrast with 6.
Line 4 should fail for the reason in the following paragraph.
The reference manual and the man page (see quote in the Premise) both tell that ..
make cd
only remove the preceding pathname component, instead of looking at the ..
of the inode entry. So the parent directory of "$my_dir/dir110/dir111
as seen by cd
(in default behaviour) is "$my_dir/dir110
, which does not contain dir020
.
I thought that this behaviour may be an exception caused by ..
being the prefix of the «dirname»
provided to cd «dirname»
. However, using ..
as an infix (line 6) contrast with this.
cd
is behaving as if cd -P
has been executed (or set -P
(or similar) command issued before), that is by not following symbolic links. With cd -P
, at 1., the current working directory would be set to $my_dir/dir010
.
set -o
tells me
[omissis]
physical off
pipefail off
posix off
[omissis]
Is my (and the ones like me in other posted questions) interpretation of the reference manual correct? What am I missing? How can the behaviour of cd
be described?
[^1]: The default behaviour of cd
is to follow symlinks
Bibliography
- Bash man page sec. "SHELL BUILTIN COMMANDS"
- Bash reference manual sec. "4 Shell Builtin Commands"
- Changing parent directory (../) with symlinks
- Path resolution depending on pwd (symlinked dirs)
- How do I cd up and down again with symlinks in bash?
$PWD
is set again every time you change directories, so you will never be in$PWD/some_other_dir
, your$PWD
will always be set to your current directory, so I don't understand what "sets the current working directory to "$PWD"/dir020, unexpected." means. Do you mean it sets it todir110/dir020
?$PWD/some_other_dir
I meant$my_dir/some_other_dir
. I'm going to correct that right now.