An environment variable is a named string stored in a process's environment block and passed on by default to its children. The shell is the most visible consumer and producer of environment variables, but every running program has them: HOME, USER, PATH, PWD, SHELL, LANG, LC_*, TERM, and so on. They provide a universal, language-agnostic way for programs to find their configuration and context.
In bash:
echo $HOME
PATH=/usr/local/bin:$PATH # extend for this shell
export EDITOR=vim # make visible to children
env # show all environment variables
unset FOO # remove one
VAR=value command # set for one command only
The distinction between shell variables and environment variables is that only exported variables are passed to child processes. Forgetting export is a common source of frustration.
Login shells read /etc/profile, ~/.bash_profile, and ~/.profile to set persistent environment variables. For graphical sessions, distributions use ~/.pam_environment or ~/.config/environment.d/. The PATH variable in particular determines which binary is found when you type a command; understanding and managing it is central to being productive at the shell.
Discussed in:
- Chapter 5: The Shell — Environment Variables
Also defined in: Textbook of Linux