C.W.K.
Stream
Lesson 01 of 10 · published

Environment Variables

~12 min · env, export, variables

Level 0Window Tourist
0 XP0/95 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

The state every program reads

Environment variables are key/value pairs every running process can read. $HOME, $PATH, $USER, $LANG — all of them shape how programs behave. printenv prints them all; echo $VAR shows one.

Set, export, unset

  • VAR=value — local to this shell only.
  • export VAR=value — passed to every child process.
  • unset VAR — remove it.
  • VAR=value cmd — set just for one invocation. HTTPS_PROXY=... curl ... is the canonical example.

Why export matters

Without export, the value lives only in the current shell. If you spawn python script.py and the script reads os.environ['VAR'], it sees nothing. Always export for tools to pick it up.

The variables you'll touch

  • $PATH — executable lookup.
  • $HOME — your home dir.
  • $EDITOR — what git commit opens (set to nvim, code -w, cursor -w).
  • $PAGER — what man uses (set to less -R).
  • $LANG / $LC_* — locale and character set.
  • $TERM — terminal type.

Where to set them

Interactive use → ~/.zshrc. Login-time only → ~/.zprofile. The full breakdown is the next lesson; for now remember that one of those files is the right place.

Code

Inspect and set·bash
printenv | head
echo $EDITOR $PAGER $SHELL
# Set for this shell only
FOO=bar
echo $FOO
# Set for children
export FOO=bar
python3 -c 'import os; print(os.environ.get("FOO"))'
Per-command environment·bash
HTTPS_PROXY=http://proxy:8080 curl https://example.com
DEBUG=1 ./run.sh
# Doesn't pollute the shell — verify
echo $DEBUG

External links

Exercise

Run printenv | head -20 and read the variables. Then export EDITOR=nvim (or your editor). Confirm with echo $EDITOR. Spawn a subshell bash and echo $EDITOR — see it propagated. Type exit to return.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.