Environment variables and PATH
Environment variables are named values available to every program you run from the shell. They configure tools, hold settings like the current user and language, and pass secrets like API keys to applications without hard-coding them.
Viewing variables
| Variable | Holds |
|---|---|
HOME |
Your home folder |
USER |
Your user name |
PATH |
Where the shell looks for programs (see below) |
SHELL |
Your shell program |
PWD |
The current folder |
LANG |
Language and encoding settings |
EDITOR |
Your preferred text editor (used by git, crontab…) |
$NAME (or ${NAME}) expands to the variable's value. Use env or printenv to list them all.
Setting variables
NAME=valuesets a shell variable, visible only to the current shell. There must be no spaces around=!export NAME=valuesets an environment variable, which is passed on to programs you start.unset NAMEremoves it.
⚠️
NAME = value(with spaces) is an error: the shell thinksNAMEis a command.
Quotes matter
- Double quotes
"…"expand variables. - Single quotes
'…'keep everything literal. ${NAME}makes it clear where the name ends:${HOME}_backup.
PATH: how the shell finds commands
When you type ls, how does the shell know which program to run? It searches each folder listed in PATH, in order:
PATH is a colon-separated list: /usr/local/bin:/usr/bin:/bin. The first match wins.
which cmdshows which file will run.type cmdalso tells you whether it's a shell builtin (likecd) or an alias.
"command not found"
This error means no folder in PATH contains a program with that name. Typical causes:
- It isn't installed (or there's a typo).
- It's installed somewhere that isn't in
PATH, like~/.local/binor./node_modules/.bin.
Fix: add the folder to your PATH, usually in ~/.bashrc or ~/.zshrc:
export PATH="$HOME/.local/bin:$PATH"To run a program in the current folder, prefix it with ./ (./script.sh), because . is deliberately not in PATH, for security.
Making variables permanent
Variables you set disappear when you close the terminal. To keep them, add the export lines to your shell's startup file:
| Shell | File |
|---|---|
| bash | ~/.bashrc (and ~/.bash_profile on macOS) |
| zsh (macOS default) | ~/.zshrc |
After editing, reload it with source ~/.bashrc (or open a new terminal).
Environment variables in apps: .env files
Applications read their configuration from environment variables. This is the 12-factor app principle. In development, it's common to keep them in a .env file:
# .env — NEVER commit this file to Git!
DATABASE_URL=postgres://user:secret@localhost:5432/app
STRIPE_API_KEY=sk_test_abc123
NODE_ENV=development// Node.js reads them via process.env
const db = process.env.DATABASE_URL;# Python reads them via os.environ
import os
db = os.environ["DATABASE_URL"]Add .env to .gitignore, and commit a .env.example with placeholder values so teammates know which variables to set.
Practice
- Create a variable
PROJECTset towebsite, then use it tocdinto~/projects/$PROJECT. - Export a variable
APP_ENV=productionand print it withprintenv. - Find out where
catandsortlive, and whethercdis a builtin. - Run
source ~/.bashrc, then use thellalias it defines.
1.What's wrong with NAME = Asha?
2.You installed a tool but get "command not found". What's the likely cause?