Working directory
The working directory is the directory from which relative paths are resolved. It is a property of the shell environment and is inherited by subshells and external utilities.
Many commands use the working directory as the default location for file operations. For example, specifying a relative path in a redirection creates the file in the working directory. The ls
utility lists files in the working directory if no operand is given.
Viewing the current working directory
The PWD
and OLDPWD
variables hold the absolute pathnames of the current and previous working directories, respectively. These variables are updated automatically when the working directory changes. If you modify or unset them manually, automatic updates are no longer guaranteed.
$ echo "$PWD"
/home/user
You can also use the pwd
built-in to print the current working directory.
$ pwd
/home/user
Changing the working directory
Use the cd
built-in to change the working directory.
$ cd /tmp
$ pwd
/tmp
$ cd /dev
$ echo "$PWD" "$OLDPWD"
/dev /tmp