Files and directories · bash builtin
pwd
Print the full path of the current working directory.
pwd [-L|-P]
pwd prints the absolute path of the directory you are in. Bash has a builtin pwd, which
is what runs when you type it; coreutils also ships /usr/bin/pwd for programs that are not
shells.
The only question pwd has to answer is what to do about symbolic links. pwd -L (the bash
builtin's default) prints the path as you navigated it, links included, taken from $PWD.
pwd -P resolves every link and prints the physical location. In scripts, "$PWD" is the same
as $(pwd) without starting a command substitution.
Examples
Show where you are
pwd
/home/dev/demo
Every example on this site starts in /home/dev/demo.
Check the directory after moving
cd data/../logs
pwd
/home/dev/demo/logs
pwd always prints a clean absolute path; the data/.. detour is gone.
Physical path through a symlink
Setup run before this example
ln -s src/lib lib-linkcd lib-link
pwd
pwd -P
/home/dev/demo/lib-link /home/dev/demo/src/lib
pwd shows the path the way you got there, through the link. pwd -P resolves the link and
shows where the directory really is.
The builtin versus /usr/bin/pwd
Setup run before this example
ln -s src/lib lib-linkcd lib-link
type pwd
/usr/bin/pwd
pwd is a shell builtin /home/dev/demo/src/lib
type confirms pwd is a shell builtin, defaulting to -L. The coreutils program
defaults to -P (unless POSIXLY_CORRECT is set), so it prints the resolved path.
Use $PWD in a script
cd src
echo "building in $PWD"
echo "came from $OLDPWD"
building in /home/dev/demo/src came from /home/dev/demo
Bash keeps $PWD and $OLDPWD current after every cd, so scripts can use them directly
instead of running $(pwd).
When the current directory has been deleted
mkdir gone && cd gone
rmdir ../gone
pwd
/usr/bin/pwd
/home/dev/demo/gone pwd: couldn't find directory entry in ‘..’ with matching i-node
The builtin still prints the remembered $PWD. /usr/bin/pwd has to work the path out on
disk by walking up through .., finds no entry for the deleted directory and exits with 1.
cd .. or plain cd gets you out.
Common pitfalls
$(pwd)in a script spawns a subshell for nothing;"$PWD"gives the same value.If a script needs its own location rather than the caller's current directory,
pwdis the wrong tool: usedirname "$(realpath "$0")"or bash's${BASH_SOURCE[0]}.
GNU vs BSD and macOS
The examples above ran the GNU/Linux version. These are the differences that matter on macOS and the BSDs (written from their documentation, not run here).
Both GNU and BSD
pwdaccept-Land-P. Shell builtins default to-L; the standalone GNUpwdprogram defaults to-PunlessPOSIXLY_CORRECTis set.
Options worth knowing
A selection, described in our own words. For the full list, see pwd(1).
| Option | What it does |
|---|---|
-L, --logical | Print $PWD as navigated, symlinks included. |
-P, --physical | Resolve all symlinks and print the real path. |
Related
cdChange the shell's current working directory.realpathPrint the absolute, canonical path of a file, with symlinks, . and .. resolved.dirnameStrip the last component from a path, leaving the directory part.pushdChange to a directory and remember the previous one on a stack, so popd can return to it.- VariablesAssigning and reading shell variables, the no-spaces rule, braces, unset vs empty, scope, readonly, and the special variables bash sets for you.