set — Toggle shell options like errexit/xtrace and set positional parameters across all 5 shells
Equivalents in every shell
set -euo pipefailBash builtin. With no args, `set` lists all shell variables. With `-` flags, toggles options. With positional args, assigns `$1`, `$2`, …. Variable assignment is `var=value`, NOT `set var value`.
set -euo pipefailBash-compatible. Zsh also offers named option syntax via `setopt`/`unsetopt` (`setopt err_exit` ≡ `set -e`), which is more readable in long scripts.
set -gx PATH /opt/bin $PATHCRITICAL DIFFERENCE: in fish, `set` is *variable assignment*. `set name value` creates a variable. There is no `set -e` for errexit — fish has no global errexit option; check `$status` per command.
Set-StrictMode -Version LatestNo `set -e` global; closest is `$ErrorActionPreference = "Stop"`. `Set-Variable name value` is variable assignment; `Set-PSDebug -Trace 1` is the `set -x` equivalent.
set VAR=valueCmd `set` is variable assignment (`set VAR=value`) and listing (`set` with no args). No shell options. Spaces around `=` are taken literally — `set VAR = value` makes a variable literally named `VAR ` with value ` value`.
Worked examples
Enable strict error handling at the top of a script
set -euo pipefailset -euo pipefail$ErrorActionPreference = "Stop"; Set-StrictMode -Version LatestTrace each command before it runs (debugging)
set -xset -xfish -x script.fishSet-PSDebug -Trace 1Set an environment variable for child processes
export FOO=barset -gx FOO bar$env:FOO = "bar"set FOO=barGotchas
- Fish's `set` is the #1 porting footgun from bash. `set -x foo` in bash enables xtrace and ignores `foo`; in fish it EXPORTS a variable named `foo` with no value. Always check the shell before pasting `set` lines.
- `set -e` (errexit) is famously leaky in bash: it does not trigger inside subshells in command substitution, inside functions called via `&&`/`||`, or for the last command of a pipe (without `pipefail`). Treat it as a hint, not a guarantee.
- `set -- arg1 arg2` resets the positional parameters `$1`/`$2`/… — useful for re-parsing args. `set -- $(cmd)` is the bash idiom for splitting command output on IFS.
- Cmd's `set VAR=value` does not trim whitespace: `set VAR =hi` creates `VAR ` (with trailing space). Always write `set VAR=value` with no spaces around `=`, or use `set "VAR=value"` to be explicit.
- PowerShell has both `Set-Variable` (shell-only) and `$env:VAR = ...` (process environment, inherited by children). `set` in PowerShell is an *alias* for `Set-Variable`, NOT bash-style options — `set -e` errors out as an unknown parameter.
WSL & PowerShell Core notes
Common tasks using set
- Enable strict mode for a shell script
Make scripts fail loudly on errors — abort on the first failed command, treat unset variables as errors, and propagate pipe failures. The opposite of "fail silently and corrupt data".
- Read user input from a prompt
Pause the script, show a prompt, and capture whatever the user types — for one-off prompts, simple wizards, and "please confirm" / "please enter your password" interactions.
- Show environment variables
List the environment variables visible to the current shell — for debugging "why isn't $FOO set", auditing what subprocesses will inherit, and discovering platform-injected variables (`$HOME`, `$PATH`, `$PSModulePath`).