export — Set an environment variable and mark it for export to child processes across all 5 shells
Equivalents in every shell
export NAME=valueBuiltin. `export VAR` (with no `=`) marks an existing variable for export. Without `export`, an assignment is shell-local and not visible to spawned commands.
export NAME=valueSame as bash. Zsh additionally allows `typeset -x NAME=value` for the same effect.
set -gx NAME valueFish has no `export` builtin. The idiomatic form is `set -gx NAME value` — global scope (`-g`) plus exported (`-x`). Note the SPACE between name and value, not `=`.
$env:NAME = "value"Sets the env var for the current process and any child processes; lasts only until the shell exits. For persistence use `[Environment]::SetEnvironmentVariable('NAME','value','User')`.
set NAME=value`set` already exports to child processes within the session. For persistence across sessions, use `setx NAME value` — it writes to the registry and takes effect in NEW windows only.
Worked examples
Set an API key in the current shell so a tool can read it
export OPENAI_API_KEY=sk-abcset -gx OPENAI_API_KEY sk-abc$env:OPENAI_API_KEY = "sk-abc"set OPENAI_API_KEY=sk-abcPrepend a directory to PATH for this session
export PATH=/opt/bin:$PATHset -gx PATH /opt/bin $PATH$env:PATH = "/opt/bin;" + $env:PATHset PATH=C:\opt\bin;%PATH%Persist an environment variable across new sessions
echo 'export NAME=value' >> ~/.bashrcset -Ux NAME value[Environment]::SetEnvironmentVariable("NAME","value","User")setx NAME valueGotchas
- Bash `export VAR=value` is one step; doing `VAR=value` then `export VAR` works, but a plain `VAR=value` line by itself does NOT export — children won't see it. This trips up `.env`-style files that omit `export`.
- Fish `set` without `-x` is INTERNAL to fish — external commands cannot see it. Always add `-x` (export) when the variable must be visible to spawned processes like `npm`, `python`, or `make`.
- PowerShell `$env:NAME = $null` REMOVES the env var, but `$env:NAME = ''` keeps it set to empty. The distinction matters for tools that check existence vs. emptiness.
- Cmd `set NAME = value` (with spaces around `=`) silently creates a variable literally named `NAME ` (trailing space) with value ` value` (leading space). The `=` must be immediately adjacent on both sides.
- `setx` writes to the registry and is NEVER visible in the current cmd window — it only affects newly opened windows. To set both at once, run `set NAME=value & setx NAME value`.
WSL & PowerShell Core notes
Common tasks using export
- Prepend a directory to PATH
Add a directory to the FRONT of PATH so commands inside it override system equivalents — for tool-version pinning (node@18 vs system node) and local-bin priorities.
- 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`).
- Source an env file into the current shell
Load variables from a `.env`-style file into the current shell session — for project-specific secrets, dev-mode flags, and tool-version locks.