Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does $PWD always equal ${${:-.}:A}

Tags:

linux

shell

zsh

Given

  • A modern Linux/UNIX/OSX
  • zsh 5+
  • setopt chase_links
  • PWD has not been set abnormally

Is

[[ "$PWD" == "${${:-.}:A}" ]]

Always true?

like image 403
PythonNut Avatar asked Nov 11 '22 03:11

PythonNut


1 Answers

No, this is not always true:

xvii:~> ls -l foo
lrwxrwxrwx 1 vinc17 vinc17 10 2014-07-05 01:12:06 foo -> bar/subdir/
xvii:~> bash
vinc17@xvii:~$ cd foo
vinc17@xvii:~/foo$ pwd
/home/vinc17/foo
vinc17@xvii:~/foo$ zsh -f
xvii% echo $PWD
/home/vinc17/foo
xvii% setopt chase_links
xvii% echo $PWD
/home/vinc17/foo
xvii% echo "${${:-.}:A}"
/home/vinc17/bar/subdir
xvii% pwd
/home/vinc17/bar/subdir

But if zsh is started in a current working directory that has no symlink segments, then the current working directory will never have symlink segments either after a cd (or equivalent), and .. or . in the current working directory are not possible either since they are resolved when changing the directory, so that $PWD and ${${:-.}:A} should be equivalent if the directory still exists (see below).

In the above example, after a cd ., zsh updates $PWD to /home/vinc17/bar/subdir. However, a cd . doesn't make both forms equivalent in all cases:

xvii% mkdir my_dir
xvii% cd my_dir
xvii% rmdir ../my_dir
xvii% echo $PWD
/home/vinc17/my_dir
xvii% echo "${${:-.}:A}"
/home/vinc17/my_dir
xvii% pwd
/home/vinc17/my_dir
xvii% cd .
xvii% echo $PWD
/home/vinc17/my_dir
xvii% echo "${${:-.}:A}"

xvii% pwd
.
xvii% echo "$(realpath .)"
.: No such file or directory
like image 92
vinc17 Avatar answered Nov 15 '22 05:11

vinc17