Viewing and Setting Environment Variables in Every Shell
I write software on Windows, Mac and occasionally Linux. I always find myself forgetting what magic words to use for different shells, and Googling usually results in a surprising amount of friction for finding exactly what I want, so I’ve compiled a quick reference below.
Windows Powershell
# list all environment variables
ls env:
# fetch a specific environment variable
$env:VariableName
# pretty print the PATH variable (one path per line)
$env:PATH -split ';'
# set an environment variable for the current session
$env:VariableName = "Value"
# delete an environment variable for the current session
Remove-Item Env:\VariableName
Windows Command Prompt (CMD)
# list all environment variables
set
# fetch a specific environment variable
echo %VariableName%
# pretty print the PATH variable (one path per line)
for %A in ("%PATH:;=" "%") do @echo %~A
# set an environment variable for the current session
set VariableName=Value
# delete an environment variable for the current session
set VariableName=
Bash/Git Bash/Zsh
# list all environment variables
printenv
# fetch a specific environment variable
echo $VariableName
# pretty print the PATH variable (one path per line)
echo $PATH | tr ':' '\n'
# set an environment variable for the current session
export VariableName=Value
# delete an environment variable for the current session
unset VariableName
If you think I’m missing your favourite shell, leave a comment with the commands, because I’ll be unable to test them.