Skip to content
shellmap

pop-locationReturn to the directory most recently pushed (PowerShell popd) across all 5 shells

Equivalents in every shell

Bashunix
popd

bash built-in. Pops the top of the directory stack and cd's into it. `popd +N` pops the Nth entry from the top (not the one at index N). Stack is per-shell; popping an empty stack is an error.

Zshunix
popd

zsh built-in. Identical to bash semantics. With `setopt AUTO_PUSHD` (every cd auto-pushes), `popd` becomes essentially "back" (undo the last cd) — a common interactive workflow.

Fishunix
popd

fish built-in. No surprises; matches bash. Fish's tab-completion of stack entries (`cd -<TAB>`) gives you an interactive picker if you want to pop NOT the top.

PowerShellwindows
Pop-Location

Aliased as `popd`. `-StackName "foo"` (pwsh 6+) pops from a named stack (must match the `Push-Location -StackName` that pushed). Pops the most-recently-pushed entry — there's no `popd +N` syntax; use `(Get-Location -Stack)[N]` + `Set-Location` for arbitrary stack-position access.

cmd.exewindows
popd

cmd built-in. If the most-recent push was a UNC path (`pushd \\server\share`), `popd` AUTOMATICALLY un-maps the temporary drive letter. If you `net use Z:` something while pushed, `popd` does NOT clean it up — you have to `net use Z: /delete` manually.

Worked examples

Return to wherever you came from

Bash
popd
PowerShell
Pop-Location
cmd.exe
popd

Pop from a named stack (isolating script state)

Bash
# No named-stack support — use a subshell
PowerShell
Pop-Location -StackName myScript

Inspect the stack before popping

Bash
dirs -v && popd
PowerShell
Get-Location -Stack; Pop-Location
cmd.exe
pushd && popd

Gotchas

  • Popping an empty stack: bash and zsh print `popd: directory stack empty` (non-zero exit code); pwsh throws `Cannot pop the location because the stack is empty` (catchable exception); cmd silently does nothing. For cross-shell scripts, check before popping: bash `[ ${#DIRSTACK[@]} -gt 1 ] && popd` / pwsh `if (Get-Location -Stack) { Pop-Location }`.
  • pwsh `Pop-Location -StackName` errors if the named stack is EMPTY OR DOESN'T EXIST — not the same as "no-op if missing". Wrap in `try { Pop-Location -StackName x } catch { Write-Verbose "stack empty" }` if it might not be present.
  • cmd `popd` after a `pushd \\server\share` un-maps the temporary drive letter — but if you `cd Z:\elsewhere` while in the pushed state and then `popd`, the un-map still happens and you're unexpectedly back at the original directory with Z: gone. Cmd doesn't track that you moved off the pushed drive.
  • On Linux/macOS pwsh, `popd` aliases to `Pop-Location` — so bash-builtin features like `popd +2` (pop the Nth entry) don't work. For bash semantics from pwsh, shell out: `bash -c "popd +2"`.
  • Long-running pwsh sessions with unnamed-stack pushes that never get popped accumulate stack entries forever — `Get-Location -Stack` shows everything pushed since session start. Periodically drain: `while ((Get-Location -Stack).Count -gt 0) { Pop-Location }`.

WSL & PowerShell Core notes

pwsh`Pop-Location` is identical across pwsh platforms. `-StackName` (pwsh 6+) is the only pwsh feature without a bash builtin equivalent (bash uses subshells for stack isolation). For scripts running on both, use `-StackName` defensively — it scopes to your script and won't interfere with the caller's interactive stack.
WSLWSL bash and Windows-side cmd/pwsh have SEPARATE stacks — `popd` in WSL doesn't pop the pwsh stack. To navigate the WSL filesystem from pwsh, push `\\wsl$\Ubuntu\path` (UNC); `popd` un-maps the temporary drive letter when you return.

Related commands