variable
Bash OLDPWD — previous directory
Records the previous working directory after a successful cd. cd - uses it and prints the selected directory. It may be unset at startup; it is not a full directory history. Run in a fresh private directory with HOME set to that directory and LC_ALL=C. Expected output (one item per line): previous=home; restored.
Formal Syntax & Structure
bash syntaxbash
${OLDPWD}Return Value
string
Current value of the variable; expansion does not independently return an exit status.
Working Example
Example Snippetbash
mkdir subcd sub || exit 1[[ $OLDPWD == "$HOME" ]] && printf 'previous=home\n'cd - >/dev/null || exit 1[[ $PWD == "$HOME" ]] && printf 'restored\n'