help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: What is the difference between $PWD and pwd?


From: Stephane Chazelas
Subject: Re: What is the difference between $PWD and pwd?
Date: Sat, 2 May 2020 22:55:47 +0100

2020-04-27 15:50:45 -0500, Eric Blake:
[...]
> They both exist, thanks to logical paths.  $PWD always tracks the shell's
> current logical path (which differs from the physical path if you changed
> directories through a symlink while using logical path tracking).
[...]

Note that $PWD and pwd without -P may not contain/report a path
to the current working directory if that current directory or
its path components have changed by other means than via the
cd/popd/pushd builtins.

bash-5.0$ pwd
/home/chazelas/1
bash-5.0$ mv ~/1 ~/2
bash-5.0$ pwd
/home/chazelas/1
bash-5.0$ echo -E "$PWD"
/home/chazelas/1
bash-5.0$ pwd -P
/home/chazelas/2
bash-5.0$ pwd
/home/chazelas/2
bash-5.0$ echo "$PWD"
/home/chazelas/1
bash-5.0$ cd .
bash-5.0$ echo "$PWD"
/home/chazelas/2


Note how "pwd" no longer reports the contents of $PWD after "pwd
-P" has been called after the current directory was renamed.

You need a "cd ." for bash to recompute a correct path to the
current working directory for both $PWD and pwd.

That was with GNU bash, version 5.0.16(1)-release
(x86_64-pc-linux-gnu)

You'll find a lot of variation between shells on that front.

-- 
Stephane



reply via email to

[Prev in Thread] Current Thread [Next in Thread]