Bash `cd -L` vs. `cd -P` vs. Bash Reference manual description
1
vote
1
answer
118
views
**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 after cd
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
1) sets the current working directory to "$my_dir"/dir110/dir111
, as expected[^1].
2) sets the current working directory to "$my_dir"/dir110
, as expected[^1].
3) back to 1.
4) sets the current working directory to "$my_dir"/dir020
, **unexpected**. I would expect cd
to fail at "$my_dir"/dir110/dir020
(see below for explanation).
5) back to the root of my experiment.
6) **same as 4**.
7) back to the root of my experiment.
8) 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?
**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?]
Asked by the_eraser
(185 rep)
Jul 24, 2024, 04:27 PM
Last activity: Jul 24, 2024, 05:21 PM
Last activity: Jul 24, 2024, 05:21 PM