unset — Remove a shell variable or function from the current environment across all 5 shells
Equivalents in every shell
Bashunix
unset FOORemoves 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.
Fishunix
set -e FOOFish has no `unset` — use `set --erase FOO` (short: `set -e FOO`). Same flag namespace as `set`.
PowerShellwindows
Remove-Variable FOOShell 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_PROXYFish
set -e HTTP_PROXYPowerShell
Remove-Item Env:HTTP_PROXYcmd.exe
set HTTP_PROXY=Remove a shell function
Bash
unset -f my_funcZsh
unset -f my_funcFish
functions --erase my_funcPowerShell
Remove-Item Function:My-FuncUnset every variable matching a prefix (env scrubbing)
Bash
for v in ${!AWS_*}; do unset "$v"; doneFish
for v in (set -nx | string match "AWS_*"); set -e $v; endPowerShell
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.