Skip to content
shellmap

push-locationSave current directory on a stack, then cd to a new one across all 5 shells

Equivalents in every shell

Bashunix
pushd /etc

bash built-in. Saves CWD on the directory stack, then `cd /etc`. `dirs` shows the stack; `popd` returns to the previous. The stack is per-shell — no persistence across sessions. Tab-completes paths. Errors silently if the target doesn't exist (returns non-zero exit code).

Zshunix
pushd /etc

zsh built-in with extras: `setopt AUTO_PUSHD` makes every `cd` auto-push (so `cd -<N>` traverses history). `pushd` alone with no arg toggles between the top two stack entries — useful for back-and-forth between two dirs. `dirs -v` numbers each stack entry for `cd ~N` jumping.

Fishunix
pushd /etc

fish built-in. Works identically to bash. Fish's tab completion of stack indices (`cd -<TAB>`) is interactive — shows the full path of each entry inline. No `AUTO_PUSHD` equivalent; explicit `pushd` is the way.

PowerShellwindows
Push-Location /etc

Aliased as `pushd`. Cross-platform: works on Windows / Linux / macOS pwsh. `-StackName "foo"` (pwsh 6+) creates / appends to a NAMED stack for isolation between scripts. `Get-Location -Stack` shows the current (unnamed) stack; `-StackName foo` shows a named one.

cmd.exewindows
pushd C:\Windows

cmd built-in. Works identically for local paths. **Killer feature**: `pushd \\server\share` (UNC path) AUTO-MAPS a temporary drive letter — Windows assigns the highest-available letter (Z:, Y:, …), changes into it, and `popd` un-maps it. Useful for one-shot UNC traversal without manual `net use`.

Worked examples

Save current dir, work elsewhere, return

Bash
pushd /etc && grep root passwd && popd
PowerShell
Push-Location /etc; Select-String root passwd; Pop-Location
cmd.exe
pushd C:\Windows && dir System32 && popd

Show the current directory stack

Bash
dirs -v
PowerShell
Get-Location -Stack
cmd.exe
pushd

Use a named stack to isolate this script's directory state

Bash
# No named-stack support — use a subshell instead: ( pushd /etc; ...; popd )
PowerShell
Push-Location /etc -StackName myScript; Pop-Location -StackName myScript

Gotchas

  • cmd's `pushd \\server\share` consumes a drive letter from the highest-available. On systems with many mapped drives, you can RUN OUT — failing with `The system cannot find drive specified`. Workaround: explicitly `net use Z: \\server\share` first, then `pushd Z:\` to control which letter is consumed.
  • pwsh `Push-Location` without `-StackName` writes to the DEFAULT (unnamed) stack — which is SESSION-WIDE, shared across every script and every interactive prompt. Long-running pwsh sessions accumulate entries indefinitely; periodically `Pop-Location` everything or use `-StackName` per-script.
  • bash `pushd` does NOT print the new directory by default (unlike `cd`) — you cd silently. The full stack is printed only because pushd then auto-runs `dirs`. To suppress, run `pushd /etc > /dev/null` and check exit code.
  • On Linux/macOS pwsh, `pushd` is an ALIAS for `Push-Location`, not the bash builtin — so `pushd -L` (logical follow-symlinks) and similar bash-specific flags don't work. For bash-builtin behaviour from pwsh, shell out: `bash -c "pushd -L /tmp && pwd"`.
  • zsh's `setopt AUTO_PUSHD` (auto-push every cd) is great for interactive use but DANGEROUS in scripts — the stack grows unbounded, `cd -N` semantics depend on session history that scripts can't predict. Always `setopt LOCAL_OPTIONS` at the top of zsh scripts to scope option changes.

WSL & PowerShell Core notes

pwsh`Push-Location` and `pushd` alias work on every pwsh platform. Drive letters are Windows-specific — `Push-Location C:\path` works on pwsh on Windows; pwsh on Linux/macOS uses Unix paths. `-StackName` (pwsh 6+) for script-isolated stacks. The default unnamed stack is session-wide; clean it on script exit with `while (Get-Location -Stack) { Pop-Location }`.
WSLWSL bash and Windows-side cmd/pwsh have SEPARATE directory stacks — pushing in WSL doesn't affect the pwsh stack and vice versa. To navigate WSL filesystem from pwsh: `Push-Location \\wsl$\Ubuntu\home\user`. UNC-style WSL paths consume a drive letter just like SMB shares.

Related commands