Skip to content
shellmap

unsetRemove a shell variable or function from the current environment across all 5 shells

Equivalents in every shell

Bashunix
unset FOO

Removes the variable from the shell. `unset -f my_func` removes a function. `unset -v FOO` is explicit for vars when a function of the same name exists.

Zshunix
unset FOO

Identical to bash, same `-f` / `-v` flags.

Fishunix
set -e FOO

Fish has no `unset` — use `set --erase FOO` (short: `set -e FOO`). Same flag namespace as `set`.

PowerShellwindows
Remove-Variable FOO

Shell variable removal. For environment variables use `Remove-Item Env:FOO` (or `$env:FOO = $null`, which clears but leaves an empty entry).

cmd.exewindows
set FOO=

No `unset`. `set VAR=` (empty value, nothing after `=`) deletes the variable. `set "VAR="` is the explicit form.

Worked examples

Remove an environment variable from the current shell

Bash
unset HTTP_PROXY
Fish
set -e HTTP_PROXY
PowerShell
Remove-Item Env:HTTP_PROXY
cmd.exe
set HTTP_PROXY=

Remove a shell function

Bash
unset -f my_func
Zsh
unset -f my_func
Fish
functions --erase my_func
PowerShell
Remove-Item Function:My-Func

Unset every variable matching a prefix (env scrubbing)

Bash
for v in ${!AWS_*}; do unset "$v"; done
Fish
for v in (set -nx | string match "AWS_*"); set -e $v; end
PowerShell
Get-Item Env:AWS_* | ForEach-Object { Remove-Item $_.PSPath }

Gotchas

  • `unset VAR` removes the variable entirely (so `${VAR-default}` substitutes the default). Setting `VAR=` only empties it (so `${VAR-default}` still returns empty). The `-` vs `:-` distinction in parameter expansion hinges on this.
  • Fish's variable model has no `unset` — coming from bash, this often surfaces as a confused `unset: command not found`. The fix is `set -e VAR` (`-e` for erase).
  • Cmd's `set VAR=` is one of the cleanest examples of cmd quirks: the missing value tells cmd to delete the variable. `set VAR= ` (with trailing space) makes the variable equal to a single space — easy typo.
  • PowerShell distinguishes shell variables (`$VAR`, lives in the `Variable:` drive) from environment variables (`$env:VAR`, lives in the `Env:` drive). `Remove-Variable VAR` only affects the shell drive; env vars need `Remove-Item Env:VAR`.
  • Unsetting `PATH` is destructive — most shells will then fail to run any external command, including the one that would put it back. Always copy first: `OLD=$PATH; unset PATH; ...; PATH=$OLD`.

WSL & PowerShell Core notes

pwshOn Linux/macOS pwsh, `Remove-Variable` and `Remove-Item Env:NAME` behave identically to Windows pwsh — the `Variable:` and `Env:` PSDrives are cross-platform. There is no `unset` alias by default; add `Set-Alias unset Remove-Variable` in `$PROFILE` if you want bash muscle-memory.
WSLInside WSL `unset` is the bash builtin and removes the variable from the WSL shell only — it does NOT affect the Windows host environment. To clear a var from cmd.exe via interop, run `cmd.exe /c "set VAR="` (the empty-RHS delete trick); `unset` does not cross the boundary.

Related commands