builtin

Bash pushd — push or rotate directory stack

Normally adds a directory and changes the working directory, or rotates saved entries. With no argument it exchanges the top two entries; -n suppresses the usual directory change. Successful calls display the stack, which can be redirected when not needed. Run in a fresh private directory with HOME set to that directory and LC_ALL=C. Expected output (one item per line): cwd=a b; depth=2.

Formal Syntax & Structure

bash syntaxbash
pushd [-n] [dir | +N | -N]

Parameters & Attributes

NameTypeDescription
dirpathDirectory to push.
+N / -NselectorRotate using the indexed stack entry.
-nflagSuppress the usual directory change.

Return Value

exit status

Zero on success; nonzero on failure.

Working Example

Example Snippetbash
mkdir 'a b'pushd 'a b' >/dev/null || exit 1printf 'cwd=%s\n' "${PWD##*/}"printf 'depth=%s\n' "${#DIRSTACK[@]}"
Bash Reference