push-location — Save current directory on a stack, then cd to a new one across all 5 shells
Equivalents in every shell
pushd /etcbash 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).
pushd /etczsh 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.
pushd /etcfish 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.
Push-Location /etcAliased 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.
pushd C:\Windowscmd 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
pushd /etc && grep root passwd && popdPush-Location /etc; Select-String root passwd; Pop-Locationpushd C:\Windows && dir System32 && popdShow the current directory stack
dirs -vGet-Location -StackpushdUse a named stack to isolate this script's directory state
# No named-stack support — use a subshell instead: ( pushd /etc; ...; popd )Push-Location /etc -StackName myScript; Pop-Location -StackName myScriptGotchas
- 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.