variable

Bash DIRSTACK — directory stack array

Contains the directory stack in display order. pushd and popd add or remove entries. Assignment to existing elements does not itself change the working directory. Unsetting the variable removes its special behavior. Run in a fresh private directory with HOME set to that directory and LC_ALL=C. Expected output (one item per line): top=work; previous=home.

Formal Syntax & Structure

bash syntaxbash
${DIRSTACK}

Return Value

indexed array

Current remembered directories; index zero is the top.

Working Example

Example Snippetbash
mkdir workpushd work >/dev/null || exit 1printf 'top=%s\n' "${DIRSTACK[0]##*/}"[[ ${DIRSTACK[1]} == "$HOME" ]] && printf 'previous=home\n'
Bash Reference