builtin
Bash dirs — inspect directory stack
Displays the remembered directory stack. Index +0 refers to its leftmost entry and -0 to its rightmost entry. -p prints one entry per line, -v adds indexes, -l avoids home abbreviation, and -c clears saved entries while retaining the current directory. Paths are not a safe line-delimited data interchange format. Run in a fresh private directory with HOME set to that directory and LC_ALL=C. Expected output (one item per line): top=sub; bottom=home; entries=1.
Formal Syntax & Structure
bash syntaxbash
dirs [-clpv] [+N | -N]Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| options | flags | Display or clear the stack. |
| index | integer selector | Select entry from left (+N) or right (-N). |
Return Value
exit status
Zero on success; nonzero on failure.
Working Example
Example Snippetbash
mkdir subpushd sub >/dev/null || exit 1[[ $(dirs +0) == '~/sub' ]] && printf 'top=sub\n'[[ $(dirs -0) == '~' ]] && printf 'bottom=home\n'dirs -cprintf 'entries=%s\n' "${#DIRSTACK[@]}"