It is not necessarily the case even when symbolic links are not used and PWD is not set by the user:
vinc17@xvii:~$ mkdir my_dir
vinc17@xvii:~$ cd my_dir
vinc17@xvii:~/my_dir$ rmdir ../my_dir
vinc17@xvii:~/my_dir$ echo $PWD
/home/vinc17/my_dir
vinc17@xvii:~/my_dir$ realpath .
.: No such file or directory
Note that under zsh, ${${:-.}:A} still gives the same answer as $PWD (the zshexpn(1) man page says about the A modifier: "Note that the transformation takes place even if the file or any intervening directories do not exist.").
Note that however, $PWD contains obsolete information. Using it may be a bad idea if some other process can remove the directory. Consider the following script:
so no, $PWD is not always the same as $(realpath .).
The bash manual indicates that the PWD variable is set by the built-in cd command. the default behaviour of cd is:
symbolic links are followed by default or with the -L option
This means that if you cd into a symlink the variable gets resolved relative to the symlink, not relative to the physical path. You can change this behavior for a cd command by using the -P option. This will cause it to report the physical directory in the PWD variable:
$ cd -P /tmp/fakedir
$ echo $PWD
/tmp/realdir
You can change the default behavior of bash using the -P option:
$ set -P
$ cd /tmp/fakedir
$ echo $PWD
/tmp/realdir
$ set +P
$ cd /tmp/fakedir
$ echo $PWD
/tmp/fakedir
This is of course notwithstanding the fact that you can assign anything you want to the PWD variable after performing a cd and it takes that value:
$ cd /tmp/fakedir
$ PWD=/i/love/cake
$ echo $PWD
/i/love/cake
It is not necessarily the case even when symbolic links are not used and
PWD
is not set by the user:Note that under zsh,
${${:-.}:A}
still gives the same answer as$PWD
(thezshexpn(1)
man page says about the A modifier: "Note that the transformation takes place even if the file or any intervening directories do not exist.").Note that however,
$PWD
contains obsolete information. Using it may be a bad idea if some other process can remove the directory. Consider the following script:It will output:
i.e.
$PWD/file
has changed.It's pretty easy to test that this is not always the case.
so no,
$PWD
is not always the same as$(realpath .)
.The bash manual indicates that the
PWD
variable is set by the built-incd
command. the default behaviour of cd is:This means that if you cd into a symlink the variable gets resolved relative to the symlink, not relative to the physical path. You can change this behavior for a
cd
command by using the-P
option. This will cause it to report the physical directory in thePWD
variable:You can change the default behavior of bash using the
-P
option:This is of course notwithstanding the fact that you can assign anything you want to the
PWD
variable after performing acd
and it takes that value:but that's not really what you were asking.