Common Linux Environment Variables

Learn what common Linux environment variables such as HOME, PATH, LANG, TERM, DISPLAY, and SHELL mean, how to inspect them, and how to set them safely.

Linux environment variables are named values associated with a process. They provide context to shells and programs, such as the current user, home directory, command search path, locale, terminal type, and graphical display.

This lesson assumes basic command-line navigation, paths, command execution, and a general understanding of parent and child processes.

What is an environment variable?

An environment variable is a named value supplied to a process. When a process starts another process, the child process normally receives a copy of the parent's exported environment. A command launched from a shell is therefore usually a child process of that shell.

A shell variable is a value known to the current shell. It is not automatically passed to child processes. The shell's export operation marks a variable for inheritance by programs that the shell launches.

project_dir="$HOME/projects"       # Shell variable only
export project_dir                  # Make it part of the environment
sh -c 'printf "%s\n" "$project_dir"'

Variables are used for user identity, home locations, command lookup, language and regional settings, terminal behavior, graphical display access, editor preferences, and application configuration. Their values are not universal: they can differ according to the distribution, shell, login method, desktop environment, remote connection, container, service manager, and process that examines them.

Viewing and inspecting variables

Print one variable

In a shell, parameter expansion uses a dollar sign followed by the variable name. Quote the expansion so spaces, wildcards, and other special characters remain part of one value.

printf '%s\n' "$HOME"
printf '%s\n' "$PATH"
printf '%s\n' "$TERM"

For an unset variable, ordinary expansion commonly produces an empty string. That means an unset variable and a set-but-empty variable can look identical unless you test explicitly.

if [ "${EDITOR+x}" = x ]; then
    printf 'EDITOR is set; value: <%s>\n' "$EDITOR"
else
    printf 'EDITOR is unavailable\n'
fi

if [ -z "${EDITOR:-}" ]; then
    printf 'EDITOR is unset or empty\n'
fi

List the environment

printenv and env list exported environment entries visible to the command. In Bash, set shows shell variables as well as functions and other shell state, while export -p shows variables marked for export.

printenv
env
set
export -p

Do not assume that a variable shown by set will be visible to another program. Check the exported environment with printenv or export -p.

Common Linux environment variables

VariableTypical purposeExample value formatImportant caveat
USERAccount name associated with the sessionsamMay be user-controlled or absent; do not use it as authoritative identity for security decisions.
HOMEUser's home directory/home/samUsed by many programs for configuration and relative user paths; validate it in privileged scripts.
PWDShell's current working directory/home/sam/projectsDescribes the shell's directory and can differ between processes.
HOSTNAMEHost name exposed to programsworkstationMay be abbreviated or set differently in containers and network contexts.
MAILConventional local mail-spool location/var/mail/samModern systems may use remote mail or no local mailbox.
SHELLPreferred login shell path/bin/bashDoes not necessarily identify the shell currently executing a script.
PS1Primary interactive Bash prompt\u@\h:\w\$ Prompt syntax is shell-specific and is mainly an interactive setting.
TERMTerminal capability typexterm-256colorIncorrect values can break colors, keys, cursor controls, or screen drawing.
HISTFILESIZEMaximum number of lines kept in a shell history file2000Distinct from the in-memory history setting, commonly controlled by Bash's HISTSIZE.
LANGDefault localeen_US.UTF-8LC_ALL and category-specific LC_* variables can override it.
TZPer-process time-zone selectionUTC or America/New_YorkIf unset, system configuration commonly supplies the default.
EDITORPreferred command-line editorviSome applications prefer VISUAL instead.
VISUALPreferred full-screen interactive editorvimMany interactive tools give it higher priority than EDITOR.
MANPATHManual-page search path/usr/local/share/man:/usr/share/manReplacing it can hide default directories unless those directories are preserved appropriately.
OSTYPEOperating-system classification supplied by Bash-like shellslinux-gnuNot universally portable and unsuitable as the sole basis for reliable platform detection.
DISPLAYX11 graphical display connection target:0 or localhost:10.0Identifies where an X client connects; it does not itself grant permission.
PATHOrdered search path for executable commands/usr/local/bin:/usr/bin:/binEarlier directories take precedence; unsafe directories can enable command hijacking.

User, host, and location variables

USER normally contains the account name associated with the session. It is useful for display and ordinary configuration, but it may be missing, inherited incorrectly, or deliberately changed. Security-sensitive code should determine identity through operating-system mechanisms rather than trusting this variable.

HOME identifies the user's home directory. Applications commonly use it to find configuration files, caches, and data. It also makes user-specific paths portable within a session.

PWD represents the shell's current working directory. A child program inherits the working directory as process state, while the value of PWD is shell-maintained metadata. HOSTNAME exposes a host name to programs, but its exact form can vary.

MAIL conventionally points to a local mailbox, often under /var/mail or /var/spool/mail. It may be unset or irrelevant on systems using webmail, remote mail, or another delivery system.

Shell and terminal variables

SHELL commonly records the user's preferred login shell, such as /bin/bash or /bin/zsh. It is not a reliable way to determine which interpreter is executing a script. A script should declare its interpreter with an appropriate shebang, and shell-specific behavior should not be assumed in a different shell.

PS1 is Bash's primary interactive prompt setting. For example, this changes the prompt in the current Bash session:

PS1='[\u@\h \W]\$ '

Prompt escapes such as \u and \w are Bash features. Other shells use different prompt syntax, so PS1 is not a universal prompt configuration.

TERM tells terminal applications which terminal capability description to use. A value such as xterm-256color influences colors, cursor movement, function keys, and full-screen drawing. An incorrect value can produce garbled output or broken key behavior. Avoid changing it arbitrarily; use the value provided by the terminal emulator or correctly configured remote connection.

HISTFILESIZE limits the number of lines Bash retains in the history file. Bash's HISTSIZE controls the in-memory history list, so changing one does not necessarily change the other.

Locale, language, and time

A locale is a collection of regional and language rules for messages, character handling, sorting, numbers, dates, and other formatting. LANG supplies the default locale selection.

locale
printf '%s\n' "$LANG"
LANG=C sort input.txt

Locale categories can be overridden individually. Examples include LC_TIME for date and time formatting and LC_COLLATE for sorting. LC_ALL has higher precedence than LANG and the individual LC_* categories. It is useful for a deliberate, temporary override, but is often a poor permanent blanket setting because it overrides every category and can cause unexpected behavior.

Locale-sensitive command output can break scripts that parse human-oriented text. Scripts should prefer stable machine-readable output where available, or deliberately use a predictable setting such as LC_ALL=C for one command when appropriate.

TZ selects a time zone for the process. This is useful for testing or for one command without changing the system's time zone.

TZ=UTC date
TZ=Asia/Tokyo date

Editor and documentation preferences

Command-line programs often consult EDITOR when they need an editor. Interactive programs commonly consult VISUAL first and fall back to EDITOR.

export EDITOR=vi
export VISUAL=vim

Both values should name an executable that can be found through PATH, or an appropriate command path.

MANPATH is an optional search path for manual pages. It is a colon-separated list of directories. Replacing it carelessly can remove directories that the manual system would otherwise search. Extend it only when you understand how the local man implementation preserves its default path.

Operating-system and session metadata

Bash-like shells may provide OSTYPE, commonly with a value such as linux-gnu. It is a shell classification rather than a universal Linux interface. Portable scripts should use more reliable, narrowly scoped detection methods when platform identification is genuinely required.

Desktop sessions, SSH logins, containers, scheduled jobs, and services can expose different metadata. A variable visible in an interactive terminal is not automatically present in a cron job or system service.

DISPLAY and graphical sessions

DISPLAY is commonly used by X11 clients to identify the X server to which they should connect. A local first display is commonly represented as :0. A value such as localhost:10.0 can identify an SSH-forwarded display: the part before the colon identifies a host, the display number identifies an X display, and an optional screen number follows a period.

DISPLAY only identifies a connection target. It does not grant access. X11 authorization data, commonly associated with Xauthority, controls whether the client may connect. In a Wayland session, DISPLAY may still exist for Xwayland compatibility, while other graphical-session variables describe the native Wayland environment.

printf '%s\n' "$DISPLAY"

PATH and command lookup

PATH is an ordered, colon-separated list of directories. When a command is entered without a slash, the shell searches these directories from left to right and normally runs the first matching executable.

printf '%s\n' "$PATH"
export PATH="$HOME/.local/bin:$PATH"   # Personal commands take precedence
export PATH="$PATH:$HOME/tools"         # Personal tools are searched later
command -v command_name
type -a command_name

Prepending a directory gives its commands precedence over commands with the same name later in PATH. Appending preserves the priority of existing system directories. Avoid adding the current directory or writable shared directories to PATH: an unintended executable could be run instead of the expected command. Use command -v to find the selected command and Bash's type -a to reveal aliases, functions, builtins, and all matching executables.

Setting, exporting, and unsetting variables

A plain assignment creates or changes a shell variable in the current shell. Exporting it makes the value available to commands launched afterward. Unsetting removes it from the current shell.

project_dir="$HOME/projects"
printf '%s\n' "$project_dir"
export project_dir
unset project_dir

A one-command assignment changes the environment of that command without changing the surrounding shell:

TZ=UTC date
LANG=C sort input.txt

Exports flow downward to child processes. A child cannot modify the environment of its parent shell after it exits. This is why running a script normally cannot permanently change the caller's variables; the script would need to be sourced, and it must use syntax compatible with the current shell.

MethodLifetimeInherited by child processesTypical use
Shell assignmentCurrent shell until changed or exitedNoInternal shell calculations and temporary state
Exported assignmentCurrent shell and descendantsYesConfigure commands launched from the session
One-command assignmentOne command invocationYes, by that command's childrenTest a locale or time zone safely
Startup-file settingFuture sessions that read that fileYes, for descendantsPersistent user shell preferences
System or service environment settingAccording to the launcher or service configurationAccording to that process treeDesktop sessions, scheduled jobs, and services

Persistence and startup files

Session-only settings disappear when the relevant shell or process ends. Persistent settings belong in an appropriate configuration source. For Bash, login shells and interactive non-login shells commonly read different startup files. The exact behavior depends on how Bash was started and on distribution conventions.

Do not assume that a setting in an interactive shell startup file reaches every context. Graphical desktop sessions, SSH logins, cron jobs, containers, and system services may use different environment sources and may not read the same files. System-wide environment configuration also depends on the distribution and initialization or service system. Choose the configuration location according to how the target process is launched.

# Example pattern in an appropriate user startup file
export PATH="$HOME/.local/bin:$PATH"
export EDITOR=vi
export VISUAL=vim

Security and scripting considerations

  • Treat environment values as user-controlled input in many contexts. Quote expansions such as "$HOME", "$PATH", and "$EDITOR" when they are intended to remain single arguments.
  • Validate values before using them in commands, filenames, interpreter selection, or configuration.
  • Do not trust USER, PATH, HOME, or DISPLAY for privilege decisions. These variables do not prove identity, authorization, or safe file ownership.
  • Administrative scripts are vulnerable to PATH hijacking if an attacker can place a command earlier in the search path. Use absolute command paths where practical or establish a controlled, trusted PATH.
  • Avoid placing secrets in environment variables. Child processes inherit them, and depending on operating-system permissions and inspection tools, process environments can sometimes be observed.

Troubleshooting common problems

SymptomLikely variable or causeDiagnostic actionTypical correction
Command not foundRequired directory is absent from PATH, or the startup file was not loaded.Print PATH; run command -v name; check the executable and permissions.Add the trusted directory using the configuration appropriate to the session.
Wrong command runsEarlier PATH entry, alias, or function shadows the intended command.Run type -a name and review PATH order.Correct the order, remove the unintended alias or function, or use an absolute path.
Unexpected language or date outputLANG, an LC_* category, or higher-precedence LC_ALL.Run locale and inspect the relevant variables.Select an installed locale or use a temporary predictable locale for scripts.
Terminal colors or keys malfunctionTERM does not match terminal capabilities or the remote terminal database.Print TERM locally and remotely; compare terminal sessions.Use the terminal's correct value and avoid arbitrary overrides.
GUI program cannot connectDISPLAY is unset or inaccessible, authorization is missing, or X11 forwarding is not configured.Inspect DISPLAY and the applicable Xauthority or forwarding configuration.Use the correct local session or correctly configured forwarding and authorization.
Editor-dependent command opens the wrong editorVISUAL or EDITOR is unset, invalid, unavailable, or prioritized differently by the tool.Inspect both variables and run command -v on the selected editor.Set a valid editor and ensure it is available through PATH.
Variable is missing in another program or serviceIt was not exported, or the target context did not inherit the interactive shell.Compare printenv in both contexts and identify the launch mechanism.Configure it in the environment source used by the target process.

Practical inspection checklist

printf 'USER=%s\n' "$USER"
printf 'HOME=%s\n' "$HOME"
printf 'PWD=%s\n' "$PWD"
printf 'SHELL=%s\n' "$SHELL"
printf 'LANG=%s\n' "$LANG"
printf 'TERM=%s\n' "$TERM"
printf 'DISPLAY=%s\n' "$DISPLAY"
printf 'PATH=%s\n' "$PATH"
locale
command -v sh
type -a sh

Environment variables describe the context in which a program runs, but they are not guaranteed constants. Inspect them in the context that matters, quote and validate them in scripts, and choose persistence settings based on the actual process launcher.

For a focused follow-up, see Common Environment Variables.