Skip to main content

How to Set an Environment Variable in the Terminal

· 8 min read
Jagdish Kumawat
Founder @ Dewiride

An environment variable is a named value that the terminal hands to every program it starts. Programs read settings and secrets from them, such as a database address or a signing key, so the values never have to sit in the code. Setting one takes a single command in any terminal.

Syntax​

In zsh and bash:

Syntax
export NAME=value
NAME=value <command>
unset NAME
PartWhat it does
export NAME=valueSets NAME for this terminal and for every program you start from it. Quote the value when it has spaces: NAME='two words'.
NAME=value <command>Sets NAME for that one command only.
unset NAMERemoves it.
$NAMEReads it, as in echo "$NAME".

In PowerShell:

Syntax
$env:NAME = 'value'
Remove-Item Env:NAME
PartWhat it does
$env:NAME = 'value'Sets NAME for this PowerShell window and every program you start from it.
$env:NAMEReads it.
Remove-Item Env:NAMERemoves it.

The Example​

The example stores a greeting in a variable named GREETING, reads it in the terminal, checks that a separate program can read it too, and removes it again.

Step 1: Set the Variable and Read It Back​

Terminal
export GREETING='Hello from the terminal'
echo "$GREETING"
Output
Hello from the terminal

Step 2: Check That Other Programs See It​

The point of an environment variable is that programs you start can read it. Start one and ask:

Terminal
printenv GREETING
Output
Hello from the terminal

printenv is a separate program, and it printed the value it received from the terminal.

In zsh and bash, a variable set without export, such as GREETING=hello, stays inside the shell, and the programs it starts don't see it. export is what passes it on.

Step 3: Set a Variable for One Command Only​

zsh and bash can set a variable for just one command, by writing it in front:

Terminal
GREETING='Just this once' printenv GREETING
echo "$GREETING"
Output
Just this once
Hello from the terminal

printenv saw the one-off value, and the terminal's own GREETING didn't change. PowerShell has no short form for this: set the variable, run the command, then set it back or remove it.

Step 4: Remove the Variable​

Terminal
unset GREETING
printenv GREETING
echo "exit status: $?"
Output
exit status: 1

printenv prints nothing for a variable that doesn't exist, and it ends with exit status 1 to say it found nothing.

A zsh terminal where export sets GREETING, echo and printenv print Hello from the terminal, a one-off GREETING prints Just this once, and after unset printenv prints nothing with exit status 1.

How Long It Lasts​

A variable set this way lives only as long as that terminal window. Close it, or open a new one, and it's gone. That's often what you want for a secret: it exists while you work and is never written to a file. To set a variable in every new terminal, put the same line in the file your shell runs when it starts:

zsh, the Mac's default shell, runs ~/.zshrc when it starts. >> adds the line to the end of the file, or creates the file when it doesn't exist:

Terminal
echo "export GREETING='Hello from .zshrc'" >> ~/.zshrc
zsh -i -c 'echo $GREETING'
Output
Hello from .zshrc

zsh -i -c starts a new zsh that reads ~/.zshrc, as a new terminal would. In bash, use ~/.bashrc instead.

A zsh terminal where echo appends export GREETING to ~/.zshrc and a new zsh prints Hello from .zshrc.

Keep secrets out of these files. Anything in them is readable by anyone who can read your files, and it ends up in backups.

Common Mistakes​

MistakeWhat happensDo this instead
Spaces around =: export GREETING = hellozsh and bash reject it, or set something elseNo spaces: export GREETING=hello
Leaving out export in zsh or bashThe shell has the value, but programs you start don'texport NAME=value
Setting the variable in one terminal and running the program in anotherThe program doesn't see itRun both in the same window, or make it permanent
A value with spaces and no quotesOnly the first word is stored, or the command failsQuote it: NAME='two words'

Troubleshooting​

SymptomLikely causeFix
A program says the variable isn't setIt was set in another terminal, or without exportSet it with export (or $env:) in the window you run the program from
printenv NAME prints nothingThe variable isn't set in this terminalSet it again, then check with printenv NAME
The variable is gone after reopening the terminalIt was set for that window onlyAdd it to ~/.zshrc, ~/.bashrc or your PowerShell profile

FAQ​

What's the difference between an environment variable and a shell variable?​

A shell variable lives inside the shell. An environment variable is also handed to every program the shell starts. export turns the first into the second, and PowerShell's $env: always makes the second.

How do I see all environment variables?​

printenv on its own lists them all in zsh and bash. In PowerShell, Get-ChildItem Env: lists them.

Conclusion​

export NAME=value sets an environment variable in zsh and bash, and $env:NAME = 'value' sets one in PowerShell. Programs started from that terminal read it, it lasts until the window closes, and unset or Remove-Item Env: removes it sooner. Put the line in ~/.zshrc, ~/.bashrc or your PowerShell profile only for values that aren't secret.

Additional Resources​