Skip to content
shellmap

exportSet an environment variable and mark it for export to child processes across all 5 shells

Equivalents in every shell

Bashunix
export NAME=value

Builtin. `export VAR` (with no `=`) marks an existing variable for export. Without `export`, an assignment is shell-local and not visible to spawned commands.

Zshunix
export NAME=value

Same as bash. Zsh additionally allows `typeset -x NAME=value` for the same effect.

Fishunix
set -gx NAME value

Fish 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 `=`.

PowerShellwindows
$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')`.

cmd.exewindows
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

Bash
export OPENAI_API_KEY=sk-abc
Fish
set -gx OPENAI_API_KEY sk-abc
PowerShell
$env:OPENAI_API_KEY = "sk-abc"
cmd.exe
set OPENAI_API_KEY=sk-abc

Prepend a directory to PATH for this session

Bash
export PATH=/opt/bin:$PATH
Fish
set -gx PATH /opt/bin $PATH
PowerShell
$env:PATH = "/opt/bin;" + $env:PATH
cmd.exe
set PATH=C:\opt\bin;%PATH%

Persist an environment variable across new sessions

Bash
echo 'export NAME=value' >> ~/.bashrc
Fish
set -Ux NAME value
PowerShell
[Environment]::SetEnvironmentVariable("NAME","value","User")
cmd.exe
setx NAME value

Gotchas

  • 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

pwshOn both Windows and Unix pwsh, `$env:NAME = "value"` works identically for the current process. Persistence diverges: Windows pwsh writes to the registry via `[Environment]::SetEnvironmentVariable`, while Linux/macOS pwsh has no persistence cmdlet — you append to your `$PROFILE` or the shell rc that launched pwsh.
WSLVariables set inside WSL stay inside WSL by default. To share specific vars with Windows interop (so `cmd.exe /c echo %X%` sees them), add them to the `WSLENV` variable: `export WSLENV=$WSLENV:MYVAR/u`. The `/u` flag converts Unix paths to Windows form on the way out.

Common tasks using export

Related commands