Environment Variables
Variables that are available system-wide to any process, not just your current shell session.
Viewing them
env # all environment variables
printenv # same thing
printenv HOME # specific variable
echo $HOME # access one directlySetting them
# temporary — only exists in current terminal session
export MY_VAR="hello"
echo $MY_VAR
# open a new terminal and try echo $MY_VAR — it's goneTo make it permanent add it to your shell config:
nano ~/.zshrc # since you use zshAdd at the bottom:
export MY_VAR="hello"
export PATH="$PATH:/some/new/path"Then reload:
source ~/.zshrc.env files
Projects store environment variables in a .env file:
nano .envDATABASE_URL=postgresql://localhost:5432/mydb
REDIS_URL=redis://localhost:6379
API_KEY=supersecretkey
PORT=3000Load it into your current shell:
source .env
echo $DATABASE_URLAlways add .env to .gitignore:
echo ".env" >> .gitignoreImportant built-in variables
| Variable | Meaning |
|---|---|
$HOME | your home directory |
$USER | current username |
$PATH | directories searched for commands |
$SHELL | your current shell |
$PWD | current directory |
$EDITOR | default text editor |
$LANG | system language |
Run these:
echo $PATH
echo $SHELL
echo $USER
export TEST_VAR="spectroniq"
echo $TEST_VAR
unset TEST_VAR
echo $TEST_VARTell me what you get.
> echo $PATH
echo $SHELL
echo $USER
export TEST_VAR="spectroniq"
echo $TEST_VAR
unset TEST_VAR
echo $TEST_VAR
/home/kijuchihe/.local/bin:/home/kijuchihe/.local/share/pnpm:/home/linuxbrew/.linuxbrew/bin:/home/kijuchihe/.bun/bin:/home/kijuchihe/.nvm/versions/node/v22.14.0/bin:/home/kijuchihe/.nvm/versions/node/v22.14.0/bin:/home/kijuchihe/.cargo/bin:/home/kijuchihe/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin
/usr/bin/zsh
kijuchihe
spectroniqAll working. A few things your $PATH reveals about your setup:
pnpm— you use pnpm as your Node package managerlinuxbrew— you have Homebrew on Linux installedbun— you have Bun runtime installednvm— Node version manager (node v22.14.0 currently active)cargo— you have Rust installed
That’s a serious dev setup.
Notice the last echo $TEST_VAR printed nothing — unset wiped it. That’s correct.
Jump to a subject
Software engineering
Infrastructure & security
Computer science
Hardware & engineering