Lesson 6.2: Environment variables
Shell variables and environment variables: setting, reading, exporting and unsetting them; the important ones ($HOME, $USER, $PATH, $PWD, $LANG); what PATH really does; and why secrets in variables can leak.
16 min
You will learn to
- Set and read a shell variable
- Explain the difference between a shell variable and an exported environment variable
- List the environment with env and printenv
- Explain and extend PATH
- Set a variable for one command only
Key terms
- Shell variable
- A name with a value, stored in the current shell only. Created with NAME=value (no spaces), read with $NAME.
- Environment variable
- A variable marked for export: a copy is passed to every program started from the shell.
- export / unset
- export NAME (or export NAME=value) puts a variable into the environment; unset NAME deletes it.
- env / printenv
- Print the current environment; printenv NAME prints one value. env NAME=value command runs one command with an extra variable.
- Important variables
- HOME your home directory, USER your name, SHELL your login shell, PWD current directory, LANG language and character set, PATH command search path, EDITOR your preferred editor.
- PATH
- Colon-separated directories searched in order for a command name. The current directory is deliberately not included, which is why you type ./script.
← Common command line shells · All lessons · Shell start-up files →