TExcelTAD
Home › Terminal & Command Line › Variables & Environment Variables

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).

text
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%:

cmd · try it yourself
@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 unset
Hello, Ada! Born 211 years ago.
Good morning, Ada.
[] [ oops]
name is now unset
  • No spaces around =. set wrong = oops creates a variable called wrong (with a trailing space) whose value is oops. The quoted form set "name=value" protects you from stray trailing spaces too.
  • set /a does 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):
cmd
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):

powershell · try it yourself
$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: []
  • $name is visible only inside PowerShell; $env:GREETING is passed to programs you start.
  • PowerShell variables keep their type: $year is 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)

bash · try it yourself
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 = Ada would try to run a command called name.
  • A plain assignment creates a shell variable. export turns it into an environment variable, so child processes (here, a second bash) can see it.
  • VAR=value command sets 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 (or printenv) lists the environment; printenv HOME prints 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?

  1. set NAME=value
  2. export NAME=value
  3. $env:NAME = value
  4. setx NAME value

What does set wrong = oops do in CMD?

  1. Sets wrong to oops
  2. Fails with a syntax error
  3. Creates a variable named wrong with the value oops
  4. Compares wrong with oops

Which is the PowerShell way to read the HOME-folder environment variable on Windows?

  1. %USERPROFILE%
  2. $USERPROFILE
  3. $env:USERPROFILE
  4. ${USERPROFILE}