POSIX shells (like bash
) and fish
make the path of the current working directory (a process property inherited by children and preserved across execution of commands) in the $PWD
special variable in POSIX shells. The equivalent for csh
-like shells is $cwd
.
The variable is dynamic. Its content may change after you call a command that changes the current directory like cd
(or pushd
/popd
in some shells). POSIX shells also maintain a $OLDPWD
variable that contains the previous working directory from before you called a successful cd
/pushd
/popd
.
The pwd
command prints one path to the current directory. In POSIX shells, that command is often built-in and just prints the content of $PWD
.
If you want to know what the working directory at the time the script started, you'll need to record it at the start of your script like:
#! /bin/sh -
original_PWD=$PWD
...
cd ...
To know the current working directory of your parent process, on GNU/Linux, you can do
parent_PWD=$(readlink -ve "/proc/$PPID/cwd")
though you may not always have permission to get that information. If your parent died, you'll be adopted by init
or by the child sub-reaper if any. However $PPID
will not be updated, so you may get the cwd of the wrong process in that case (if the the pid of your parent has been reused).