Linux Command Reference ยท Bash Syntax
Common environment variables
Reference for the environment variables every Linux user meets: PATH, HOME, USER, SHELL, PWD, IFS, LANG, EDITOR, TMPDIR and PS1, with what each one does.
Environment variables are named strings that every process inherits from its parent. Programs read them to find out where to look for commands, where your home folder is, which language you speak and which editor you prefer. Bash also uses a few of them (IFS, PS1, PATH) to control its own behavior.
By convention the names are uppercase. A variable only reaches child processes if it has been exported; otherwise it is a plain shell variable. See export.
echo "$HOME" # your home directory echo "$PATH" # colon-separated search path printenv USER # print one environment variable
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
NAME=value command # set for one command only export NAME=value # set for this shell and its children printenv [NAME] # show exported variables
| Variable | Description |
|---|---|
PATH |
Colon-separated directories searched for commands. The first match wins. An empty entry means the current directory, which is a security risk. |
HOME |
Your home directory. ~ and cd with no argument use it. |
USER, LOGNAME |
Your login name (set by the login process, so scripts under cron or containers may lack them). |
SHELL |
The user's login shell, not necessarily the shell running now. |
PWD, OLDPWD |
Current and previous working directory. cd - uses OLDPWD. |
IFS |
Internal field separator for word splitting and read. Default: space, tab, newline. |
PS1, PS2, PS4 |
Prompt strings (primary, continuation, trace). See Prompt strings. |
LANG, LC_ALL, LC_* |
Locale: language, sorting, character encoding. LC_ALL overrides the rest. |
EDITOR, VISUAL |
Preferred text editor for programs that open one (git commit, crontab -e). |
PAGER |
Program used to page long output, for example less. |
TERM |
Terminal type, used to decide colors and cursor control. |
TMPDIR |
Directory for temporary files, when set; the default is /tmp. |
TZ |
Time zone for date and many programs. |
HISTFILE, HISTSIZE |
Where Bash saves history and how many lines it keeps. |
LD_LIBRARY_PATH |
Extra directories for shared libraries (use sparingly). |
Edge cases: exported names may not contain =. A variable that is exported with an empty value is set (to an empty string), which is different from unset; test with ${VAR+set}.
Usage notes
- Never put
.or an empty element inPATHon a multi-user machine; an attacker can plant a program named like a common command in a directory you visit. - Prefer appending or prepending to
PATH(PATH="$HOME/bin:$PATH") instead of replacing it, or standard commands disappear. NAME=value cmdsets a variable for that one command only, which is the safest way to changeLC_ALL,TZorIFStemporarily.LC_ALL=Cgives fast, predictable byte-wise sorting and ASCII-only character classes. Use it in scripts that must behave the same everywhere (seesort).- Changes made in a script do not reach your terminal. A child process cannot modify its parent's environment; use
sourceto apply them. - Use
envandprintenvto inspect what a command will really receive. Seeenv. USERandHOMEcan be wrong aftersudo. Behavior depends on the sudo configuration; do not assume.- Persistent settings go in a startup file; see Startup files.