64

So Wikipedia (link) tells me that the command pwd is short for "print working directory", and that makes sense.

But for the environment variable, the "P" has to be an acronym for something else than print.

I hear people talking about "current working directory", which sounds better and is more intuitive, but still the environment variable seems to be called $PWD, and not $CWD. Nobody ever says "Did you check the print working directory variable?".

I am currently playing around with the web application server uWSGI, and when running it tells me (on the uWSGI stats page):

"cwd":"/home/velle/greendrinks",

so they obviously like the (more intuitive acronym) cwd over pwd.

I guess I am trying to figure out if I misunderstood something, or if it is just a matter of having given the environment variable an unintuitive name?

Kusalananda
  • 333,661

2 Answers2

50

That depends on what you're doing.  First of all, $PWD is an environment variable and pwd is a shell builtin or an actual binary:

$ type -a pwd
pwd is a shell builtin
pwd is /bin/pwd

Now, the bash builtin will simply print the current value of $PWD unless you use the -P flag.  As explained in help pwd:

pwd: pwd [-LP]

    Print the name of the current working directory.

    Options:

      -L
          print the value of $PWD if it names the current working directory
      -P
          print the physical directory, without any symbolic links

    By default, ‘pwd’ behaves as if ‘-L’ were specified.

The pwd binary, on the other hand, gets the current directory through the getcwd(3) system call which returns the same value as readlink -f /proc/self/cwd.  To illustrate, try moving into a directory that is a link to another one:

$ ls -l
total 4
drwxr-xr-x 2 terdon terdon 4096 Jun  4 11:22 foo
lrwxrwxrwx 1 terdon terdon    4 Jun  4 11:22 linktofoo -> foo/
$ cd linktofoo
$ echo $PWD
/home/terdon/foo/linktofoo
$ pwd
/home/terdon/foo/linktofoo
$ /bin/pwd
/home/terdon/foo/foo

So, in conclusion, on GNU systems (such as Ubuntu), pwd and echo $PWD are equivalent unless you use the -P option, but /bin/pwd is different and behaves like pwd -P.

Source https://askubuntu.com/a/476633/291937

Thushi
  • 9,498
  • 3
    @llua Nothing forbids PWD to be an environment variable. It is actually the default case with most if not all shells and that makes sense to provide it to children processes. – jlliagre Dec 19 '14 at 12:02
  • 1
    So there is both a shell built-in pwd and a binary /bin/pwd, why have the latter, when the first is there? – Mads Skjern Dec 19 '14 at 12:11
  • 3
    I wonder why people downvoted, it seems like a good answer, and thoroughly explained. – Mads Skjern Dec 19 '14 at 12:12
  • 4
    @MadsSkjern Most builtins are required by the POSIX standard to be also available as commands. This is to allow calling them directly from other programs without the need to launch a shell. – jlliagre Dec 19 '14 at 12:23
  • 7
    This doesn't really answer the question to me. The question isn't really about the differences between $PWD and pwd (which have already been covered on U&L before I believe), but about the reasons which led to naming the variable "PWD". – John WH Smith Dec 19 '14 at 13:15
  • 2
    @JohnWHSmith If you downvoted for that reason which is perfectly valid, you would have better also add a comment earlier explaining why. – jlliagre Dec 19 '14 at 13:36
  • 2
    @jlliagre I did not, I preferred upvoting the other instead. The downvote's motives are still to be discovered :) – John WH Smith Dec 19 '14 at 13:38
  • 1
    @jlliagre : PWD = Present Working Directory I guess – Thushi Dec 19 '14 at 13:49
  • 5
    @Thushi You are more than likely correct. PWD was introduced by ksh88 which document it as: PWD The present working directory set by the cd command. You should add this to your reply. – jlliagre Dec 19 '14 at 14:09
  • 3
    @Thushi "the bash builtin will simply print the current value of $PWD" This is FALSE. pwd always print the actual working directory. A simple manual modification of the env var such as PWD=nonsense (without changing directory) will NOT change the output of pwd while echo $PWD will however output nonsense. – Atralb Nov 09 '20 at 18:47
31

$PWD is the Pathname of the current Working Directory.

When there are symbolic links, there could be different results from reading $PWD compared to executing the command pwd.

jcbermu
  • 4,736
  • 18
  • 26