Environment variables and PATH on Linux and Windows
In short: Environment variables are named values that programs inherit at start; PATH defines in which folders the shell searches for programs, and .env files supply configuration.
In more detail: Every process has an environment: key-value pairs such as HOME, USER, LANG, PATH. Child processes inherit them. They serve for configuration (database URL, mode) and secrets (see secrets), without writing values into the code.
In Depth
Linux and macOS
echo $HOME $PATH
export API_URL=https://example.org # for this shell and child processes
API_URL=x node app.js # only for this command
env | sort # show all
unset API_URLPermanently: in ~/.bashrc, ~/.zshrc, ~/.profile (login shell), system-wide /etc/environment. Changes take effect with source ~/.bashrc or a new terminal. Services (systemd) get them via Environment=/EnvironmentFile= in the unit, not through your shell.
PATH
PATH is a list of folders (Linux :, Windows ;). If you type python, the shell searches front to back; the first hit wins.
export PATH="$HOME/.local/bin:$PATH" # prepend own folder
which -a python # all hitsTypical problems: wrong version first, program not found (command not found), spaces/errors in the path. Never append . (current folder) to the PATH.
Windows
- Show:
echo %PATH%(cmd),$env:PATH(PowerShell). - Set (temporary):
set NAME=value(cmd),$env:NAME = "value"(PowerShell). - Permanently: Control Panel → Edit environment variables (user/system) or
setx NAME value; then new terminal. - Paths with spaces in quotes.
In programs and projects
- Reading:
process.env.NAME(Node),os.environ["NAME"](Python),System.getenv(Java). - .env files (dotenv) load variables for development; never into the repository (
.gitignore), add a.env.examplewithout values. - 12-factor principle: configuration via environment, different per environment (dev/test/prod).
- Hosting (Vercel, Docker, CI) has its own settings; variables with
NEXT_PUBLIC_/VITE_end up in browser code (never secrets!). - When debugging check whether the variable is set in the right process.
See also: env files, Managing secrets, command not found