Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Files and Directories

Linux pwd command

Reference for pwd: print the current working directory, the difference between logical (-L) and physical (-P) paths, and how it relates to $PWD.

pwd prints the absolute path of the current working directory. Bash has it as a builtin, and a separate program /bin/pwd exists as well. The two can differ in one respect: how they treat symbolic links in the path.

The builtin reads the shell variable PWD, which cd keeps up to date. If you reached the directory through a symlink, the default (-L, logical) output still shows the link name. With -P (physical) every symlink is resolved and you see the real location on disk.

Terminal
pwd [-L|-P]

Try it

Bash Logical versus physical

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Using pwd in scripts

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
pwd [-L | -P]
Name Description
-L Print the logical path as stored in $PWD, keeping symlink names. This is the default for the builtin.
-P Print the physical path with all symlinks resolved.

Returns: 0 on success; non-zero if the current directory cannot be determined or the options are invalid.

Usage notes

  • pwd versus $PWD: $PWD is the same text without starting a command. Inside a script, "$PWD" is the cheapest way to read the current directory; $(pwd) works but adds a subshell.
  • The program /bin/pwd (GNU coreutils) defaults to physical output unless POSIXLY_CORRECT is set, while the builtin defaults to logical. Use pwd -P or pwd -L explicitly when it matters.
  • pwd -P and realpath . give the same result for the current directory. For any other path use realpath.
  • Quote the result. Directory names can contain spaces: cd "$(pwd)", not cd $(pwd).
  • Deleted directories: if the current directory was removed, pwd -L may still print the old name from $PWD, but pwd -P and most commands fail.
  • The script's own location is not pwd. pwd shows where you are when the script runs, not where the script file lives; use dirname "${BASH_SOURCE[0]}" for that (see dirname).
  • Prompts: \w and \W in PS1 show $PWD, with ~ for your home directory.