builtin

Bash popd — remove directory stack entry

Removes the top entry by default and normally changes to the new top. Indexed removal can remove a non-top entry without changing the current directory. -n suppresses the normal directory change. An empty saved stack or invalid index causes failure. Run in a fresh private directory with HOME set to that directory and LC_ALL=C. Expected output (one item per line): restored; depth=1.

Formal Syntax & Structure

bash syntaxbash
popd [-n] [+N | -N]

Parameters & Attributes

NameTypeDescription
+N / -NselectorChoose entry to remove.
-nflagSuppress the normal directory change.

Return Value

exit status

Zero on success; nonzero on failure.

Working Example

Example Snippetbash
mkdir workpushd work >/dev/null || exit 1popd >/dev/null || exit 1[[ $PWD == "$HOME" ]] && printf 'restored\n'printf 'depth=%s\n' "${#DIRSTACK[@]}"
Bash Reference