Variables & Environment Variables
A variable is a named value the shell remembers: a folder you use often, a version number, a user name. An
environment variable is a variable that is also handed down to every program the shell starts. That is how
programs learn about your home folder, your language settings or where to find other programs (PATH, next
lesson).
your shell child process (e.g. python, git, node)
name=Ada (shell only) -- not visible
GREETING=hi (environment) --> GREETING=hi
PATH=... (environment) --> PATH=...Every OS has a set of standard environment variables. The names differ between Windows and Unix-like systems:
| Meaning | Windows | Linux / macOS |
|---|---|---|
| Your home folder | USERPROFILE (C:\Users\ada) |
HOME (/home/ada, /Users/ada) |
| Your user name | USERNAME |
USER |
| Program search path | Path |
PATH |
| Temporary folder | TEMP, TMP |
TMPDIR (macOS), usually /tmp |
| Computer name | COMPUTERNAME |
HOSTNAME (bash sets it) |
| Current shell | ComSpec (cmd.exe) |
SHELL |
On Windows the names are case-insensitive (%path% and %PATH% are the same); on Linux and macOS they are
case-sensitive ($HOME works, $home is a different, empty variable).
Windows (CMD)
In CMD every variable is an environment variable. Set it with set and read it with %name%:
@echo off
set name=Ada
set /a year=2026 - 1815
echo Hello, %name%! Born %year% years ago.
set "greeting=Good morning"
echo %greeting%, %name%.
set wrong = oops
echo [%wrong%] [%wrong %]
set name=
if not defined name echo name is now unsetHello, Ada! Born 211 years ago.
Good morning, Ada.
[] [ oops]
name is now unset
- No spaces around
=.set wrong = oopscreates a variable calledwrong(with a trailing space) whose value isoops. The quoted formset "name=value"protects you from stray trailing spaces too. set /adoes integer arithmetic.set name=deletes a variable.- Changes vanish when the window closes. To store a variable permanently for your user, use
setx(it affects new windows only, not the current one):
C:\Users\ada> setx EDITOR "code --wait"
SUCCESS: Specified value was saved.Inside if blocks and for loops, %name% is expanded too early; that is what setlocal enabledelayedexpansion
and !name! fix. See Batch Variables and setlocal.
Windows (PowerShell)
PowerShell keeps its own variables ($name) separate from environment variables, which live on the env: drive
($env:NAME):
$name = "Ada"
$year = 2026 - 1815
"Hello, $name! Born $year years ago."
$env:GREETING = "Good morning"
"$env:GREETING, $name."
"Is GREETING in the environment? " + (Test-Path env:GREETING)
"Is name in the environment? " + (Test-Path env:name)
Remove-Item env:GREETING
"After removal: [$env:GREETING]"Hello, Ada! Born 211 years ago.
Good morning, Ada.
Is GREETING in the environment? True
Is name in the environment? False
After removal: []
$nameis visible only inside PowerShell;$env:GREETINGis passed to programs you start.- PowerShell variables keep their type:
$yearis an integer, not text. - To persist a user variable on Windows:
[Environment]::SetEnvironmentVariable("EDITOR", "code --wait", "User"), or put$env:EDITOR = "..."in your profile (see Aliases & Profiles).
Linux & macOS (bash/zsh)
name=Ada
year=$(( 2026 - 1815 ))
echo "Hello, $name! Born $year years ago."
GREETING="Good morning"
bash -c 'echo "child sees GREETING=[$GREETING]"'
export GREETING
bash -c 'echo "child sees GREETING=[$GREETING]"'
LANG=C bash -c 'echo "one-off LANG for this command: $LANG"'
echo "my home folder is $HOME"
unset name
echo "name is now [${name:-unset}]"Hello, Ada! Born 211 years ago.
child sees GREETING=[]
child sees GREETING=[Good morning]
one-off LANG for this command: C
my home folder is /home/learner
name is now [unset]
- No spaces around
=here either:name = Adawould try to run a command calledname. - A plain assignment creates a shell variable.
exportturns it into an environment variable, so child processes (here, a secondbash) can see it. VAR=value commandsets a variable for one command only, a handy pattern you'll see in many READMEs.${name:-default}substitutes a default when the variable is empty or unset.env(orprintenv) lists the environment;printenv HOMEprints one variable.- To keep a variable, add
export EDITOR="code --wait"to~/.bashrc(Linux, bash) or~/.zshrc(macOS, zsh).
zsh uses the same syntax for all of this. One difference: zsh does not split unquoted variables into words, which the Quoting & Escaping lesson shows.
Comparison
| Task | CMD | PowerShell | bash / zsh |
|---|---|---|---|
| Set a variable | set name=Ada |
$name = "Ada" |
name=Ada |
| Read it | %name% |
$name |
$name or ${name} |
| Set an env variable | set NAME=Ada |
$env:NAME = "Ada" |
export NAME=Ada |
| Read an env variable | %NAME% |
$env:NAME |
$NAME |
| Delete | set NAME= |
Remove-Item env:NAME |
unset NAME |
| List all env variables | set |
Get-ChildItem env: |
env |
| Persist | setx NAME value |
[Environment]::SetEnvironmentVariable(...) / profile |
line in ~/.bashrc / ~/.zshrc |
| Arithmetic | set /a x=1+2 |
$x = 1 + 2 |
x=$((1 + 2)) |
Tip: Never store passwords or API tokens in shell startup files that you share or commit. Environment variables are visible to every program you start from that shell.
Learn more: Bash Variables, Bash Environment, PowerShell Environment Variables.
Test yourself
In bash, how do you make a variable visible to programs started from the shell?
- set NAME=value
- export NAME=value
- $env:NAME = value
- setx NAME value
What does set wrong = oops do in CMD?
- Sets wrong to oops
- Fails with a syntax error
- Creates a variable named
wrongwith the valueoops - Compares wrong with oops
Which is the PowerShell way to read the HOME-folder environment variable on Windows?
- %USERPROFILE%
- $USERPROFILE
- $env:USERPROFILE
- ${USERPROFILE}