← All cheatsheets

Shell

Environment variables — the shell, .env, and 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 `~/.zshrc` does not affect already-open shells until you `source` it.
- Subprocesses (`cmd`) see your exported env. Sibling shells (new terminals) only see what's in `~/.zshrc` / `~/.bashrc`.