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: Eric Blake
Subject: Re: What is the difference between $PWD and pwd?
Date: Mon, 27 Apr 2020 15:50:45 -0500
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.7.0

On 4/27/20 3:39 PM, Peng Yu wrote:
Hi,

Suppose that PWD is not manually set, I think $PWD and pwd should give
the same results. If this is true, I don't understand why there are
both $PWD and pwd. Does anybody know why both are defined?

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). The pwd executable has two modes, logical (repeat the contents of $PWD, if it seems to be accurate) and physical (output a result that contains no symlinks). Which mode is default (for both 'cd' and for 'pwd') is not specified by POSIX; bash defaults to logical mode, although some people detest the fact that under logical mode 'cd ..' and 'ls ..' can end up acting on different directories and prefer to always use physical mode. If you always use physical mode, then you are right that the two should never differ.


Or they do not always give the same result? Thanks.

$ cd /tmp
$ mkdir a
$ ln -s a b
$ cd -L /tmp/b
$ echo $PWD
/tmp/b
$ pwd -L
/tmp/b
$ pwd -P
/tmp/a

--
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org




reply via email to

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