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?
Command Line – Difference in Use Between pwd and $PWD
command line
Related Question
- Command Line – Difference Between ‘dir’ and ‘ls’ Commands
- Ubuntu – difference between bash.bashrc and /etc/environment file
- Set vs Export vs Env – Differences and Usage
- Difference Between TTY and GNOME Terminal – Explained
- Ubuntu – the difference between standard syntax and BSD syntax
- Command-Line – Difference Between $ and # in Linux Environment
- Ubuntu – the difference between env, declare and compgen -v
- Ubuntu – the difference between find with -exec and xargs
Best Answer
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
.