To print current/present working directory environment variable $PWD
and command pwd
are available. So, What is difference in usage of both? or What should be chose for specific purpose?
To print current/present working directory environment variable $PWD
and command pwd
are available. So, What is difference in usage of both? or What should be chose for specific purpose?
That depends on what you're doing. First of all,
$PWD
is an environment variable andpwd
is a shell builtin or an actual binary:Now, the bash builtin will simply print the current value of
$PWD
unless you use the-P
flag. As explained inhelp pwd
:The
pwd
binary, on the other hand, gets the current directory through thegetcwd(3)
system call which returns the same value asreadlink -f /proc/self/cwd
. To illustrate, try moving into a directory that is a link to another one:So, in conclusion, on GNU systems (such as Ubuntu),
pwd
andecho $PWD
are equivalent unless you use the-P
option but/bin/pwd
is different and behaves likepwd -P
.Both will return the same result if you use them without options for all working directories, including when in symbolic links.
However, from
man pwd
:This would mean executing
pwd -P
when in symbolic links which point to some other directories will print the path to the original directory.As an example, if you have a symbolic link
/var/run
that points to/run
and you are currently in/var/run/
directory, executingwill return:
and will be the same for
pwd
. However, if you execute:will return
Thus, it depends which path you require: the actual path without symbolic links or the present directory ignoring the symbolic links. The only difference between between
pwd -P
andecho $PWD
is when there are symbolic links.