Shell
Environment variables — the shell, .env, and gotchas
Environment variables in the shell: set, export, unset and persist them, load .env files safely, and avoid inheritance gotchas.
See env
env — print all env vars
printenv NAME / echo $NAME — print one
env | grep NAME — filter
Set in current shell
export NAME=value — set + export to children
NAME=value — set in shell only (not exported)
NAME=value cmd — set just for that command
Unset
unset NAME — remove
Persist
echo 'export NAME=value' >> ~/.zshrc (or .bashrc)
source ~/.zshrc — reload
Load a .env file safely
set -a; source .env; set +a — auto-export every var defined
# don't do: export $(grep -v '^#' .env | xargs) — breaks on spaces and quotes
Show with safety
env | grep -i secret — find secret-looking names
echo "$SOME_TOKEN" | head -c 8 — first 8 chars only (never leak full value)
Inheritance gotcha
- Each new shell inherits the parent shell's env. Editing
~/.zshrcdoes not affect already-open shells until yousourceit. - Subprocesses (
cmd) see your exported env. Sibling shells (new terminals) only see what's in~/.zshrc/~/.bashrc.