VMware ESXi and vSphere Cluster Management
Linux Environment Variables: Display, Set, Export, Persist, and Remove Variables
Learn how Linux environment variables work in Bash: inspect, create, expand, export, persist, troubleshoot, and unset variables.
What Are Linux Environment Variables?
An environment variable is a named value supplied to a process to describe or configure its runtime environment. Examples include the current user's home directory, the directories searched for commands, the preferred language, and the type of terminal in use.
Programs read environment variables instead of hard-coding paths or user-specific settings. For example, a program can use HOME to find a user's configuration directory without assuming that every user has the same home-directory path.
Values can differ between users and sessions. For example, HOME might be /home/alex for one user and /home/sam for another. System-wide startup configuration can provide defaults, while user-level configuration can override or extend those defaults.
Shell Variables and Environment Variables
A shell variable is maintained by the current shell. It becomes part of the process environment only after it is exported. The Bash export builtin marks a variable for inheritance by child processes.
| Characteristic | Shell variable | Exported environment variable |
|---|---|---|
| Visible in the current shell | Yes | Yes |
| Inherited by child programs | No | Yes |
| How it is created | NAME=value | NAME=value, followed by export, or export NAME=value |
| How it is made available to child processes | It must be exported | It is already marked for inheritance |
| Typical inspection method | echo "$NAME" in the current shell | echo "$NAME" or env in a child program |
Users, Sessions, and Child Processes
A shell is a command interpreter such as Bash. When Bash starts, it receives an environment from the process that launched it. The shell can create or change its own variables, and exported variables are passed to programs that the shell starts.
A child process is a program started by another process. A child normally receives a copy of the parent's exported environment at startup. Later changes in the parent do not automatically change an already-running child.
Separate terminal windows or tabs commonly run independent shell sessions. A variable created in one session is not automatically available in another. A new shell can receive variables from its launcher or from startup files, but it does not share the live shell-variable table of an unrelated shell.
Viewing the Current Environment
Use env to list variables exported to a command in the current shell context:
env
The output commonly includes entries such as HOME, PATH, USER, and SHELL. It represents the environment available when env is launched, including values inherited by the current shell and values exported by it.
To inspect one value, use variable expansion with echo:
echo "$HOME"
echo "$PATH"
Variable expansion means that Bash replaces a reference such as $HOME with the value stored in HOME before it runs the command. Quoting the expansion is a safe habit, especially when a value might contain spaces.
Common Linux Environment Variables
| Variable | Typical meaning | Example use |
|---|---|---|
HOME | The current user's home-directory location | cd "$HOME" |
PATH | A colon-separated list of directories searched for executable commands | Running a command without typing its full path |
USER | The current username | Displaying or selecting user-specific settings |
SHELL | The user's usual command shell | Identifying the configured shell program |
PWD | The shell's current working directory | Showing the directory in which commands run |
LANG | The locale used for language and formatting defaults | Influencing messages, sorting, and character handling |
TERM | The terminal type or terminal capability profile | Helping terminal programs choose display behavior |
Creating a Shell Variable
Use an assignment with the form NAME=value:
VAR1=example
echo "$VAR1"
The assignment creates or updates VAR1 in the current shell. There must be no spaces around the equals sign. Bash interprets spaces as command separators, so VAR1 = example is not a valid assignment.
An assignment alone creates a shell variable. It is available to commands that expand it in the current shell, but it is not automatically passed to programs launched afterward.
Safe Names and Values
Use conventional uppercase names for user-defined environment variables, such as PROJECT_LABEL. A valid shell variable identifier begins with a letter or underscore and then contains letters, digits, and underscores. Names cannot contain spaces or hyphens.
Quote values that contain spaces or shell-special characters:
PROJECT_LABEL="Sample Project"
echo "$PROJECT_LABEL"
Without quotes in the assignment, the space would separate shell words. Also quote an expansion when passing it as one command argument, for example printf '%s\n' "$PROJECT_LABEL".
Exporting Variables to Child Processes
Use export to mark an existing shell variable for inheritance:
VAR1=example
export VAR1
You can assign and export in one command:
export VAR1=example
export PROJECT_LABEL="Sample Project"
Only programs started after the export operation receive the updated value. Export does not change unrelated shells that are already running, such as a shell in another terminal.
Verify Inheritance
This example first creates a shell-only variable, then checks whether a child Bash process receives it:
VAR1=example
bash -c 'env | grep "^VAR1="'
export VAR1
bash -c 'env | grep "^VAR1="'
Before export VAR1, the first child normally prints nothing for VAR1. After export, the child receives and prints VAR1=example. The child process receives a copy; changing its copy does not change the parent shell's variable.
Referencing Values with Variable Expansion
Use a dollar sign when reading a value in a command:
echo "$VAR1"
echo "The project is $PROJECT_LABEL"
The dollar sign is used for expansion, not for the variable's identifier in an assignment or shell builtin:
| Purpose | Correct form | Why |
|---|---|---|
| Assign a value | VAR1=example | The name is on the left side of the assignment |
| Export by name | export VAR1 | export receives the identifier |
| Read a value | echo "$VAR1" | $VAR1 expands to the stored value |
| Remove by name | unset VAR1 | unset receives the identifier |
Session Scope and Terminal Behavior
Variables set interactively normally last only for the lifetime of the current shell. Closing that shell removes its shell variables and its changes to the environment.
- In terminal A, run
export VAR1=example. - Run
echo "$VAR1"in terminal A; it displaysexample. - Open terminal B and run
echo "$VAR1". Unless another configuration supplied the variable, it is empty. - Run
bash -c 'echo "$VAR1"'in terminal A; the child Bash receives the exported value.
This demonstrates three different contexts: the current shell, child processes started by that shell, and independent shell sessions. Export controls inheritance from a shell to its future children; it does not broadcast values to other shells.
Persisting Variables in Bash
Interactive assignments are temporary. To load a variable automatically in future Bash sessions, add an assignment or export command to an appropriate startup file. A startup file is a shell configuration script read when a shell starts under particular conditions.
For a user-specific setting, place the command in a Bash startup file under the user's home directory. Common choices include ~/.bashrc for interactive non-login Bash shells and a login-shell file such as ~/.bash_profile or ~/.profile, depending on the system's Bash configuration.
For a system-wide default, an administrator can use the system's global Bash configuration, such as a global profile or Bash configuration file. Exact filenames and which files are chained together can vary by Linux distribution, so inspect the existing configuration before editing it.
export PROJECT_LABEL="Sample Project"
After saving the command in the appropriate file, start a new matching shell and verify it:
echo "$PROJECT_LABEL"
env | grep '^PROJECT_LABEL='
You can explicitly reload a file in the current shell with the source builtin when appropriate:
source ~/.bashrc
Reloading executes the file in the current shell. It does not make a login-only file apply to every shell type. Login shells and interactive non-login shells can read different startup files, so choose the file based on where the variable is needed.
| Configuration location | Scope | Typical shell/session use | Persistence effect |
|---|---|---|---|
| User-level Bash configuration | One user's settings | That user's future Bash sessions | Persists for the user when the applicable file is read |
| System-wide Bash configuration | All users, subject to permissions and overrides | System login or Bash sessions | Provides a global default for applicable shells |
| Login-shell configuration | User or system login environment | Login shells and login sessions | Loads when a matching login shell starts |
| Interactive non-login-shell configuration | User or system interactive shell settings | New terminal shells that are not login shells | Loads when a matching interactive shell starts |
Removing Variables with unset
Use unset followed by the identifier without a dollar sign:
unset VAR1
echo "$VAR1"
After unsetting, the expansion normally produces an empty string. This removes the variable from the current shell. It does not delete a definition stored in a startup file.
export VAR1=example
unset VAR1
# A new applicable shell may recreate VAR1 if its startup file contains:
# export VAR1=example
To remove a persistent variable, delete or comment out its startup-file definition, then start a new matching shell or reload the edited file where appropriate.
Managing PATH Safely
PATH is a colon-separated list of directories that Bash searches for executable commands. It is important because it lets you run a command by name instead of typing its complete path.
echo "$PATH"
export PATH="$HOME/bin:$PATH"
The second command adds $HOME/bin while preserving the existing path. Overwriting PATH with only one directory can make standard commands appear to be missing. After changing it, verify the result with echo "$PATH" and test the commands you need.
Troubleshooting Environment Variables
The value works with echo but not in a program
The variable was probably assigned but not exported. Run export NAME, then start the program again. Exporting does not modify a program that is already running.
The variable is missing in another terminal
The assignment probably affected only the original shell. Put the export command in the appropriate user or global Bash startup file if future sessions need it.
Bash reports an invalid assignment or command-not-found error
Check for spaces around the equals sign and invalid identifier characters. Use a form such as PROJECT_MODE=debug, not PROJECT-MODE = debug.
A value containing spaces is split
Quote the assignment and the later expansion:
export PROJECT_LABEL="Sample Project"
printf '%s\n' "$PROJECT_LABEL"
A persistent value does not appear
The current shell may not have reread the file, or the file may apply to a different shell type. Start a new applicable shell, or reload the relevant file, and determine whether the shell is a login or interactive non-login shell.
unset does not seem permanent
A startup script may recreate the variable whenever a new shell starts. Unset it for the current session and also remove or change its persistent definition.
Quick Reference
# List exported variables
env
# Inspect one value
echo "$HOME"
# Create a shell-only variable
VAR1=example
# Expand a value
echo "$VAR1"
# Export an existing variable
export VAR1
# Assign and export in one command
export VAR1=example
# Export a value containing spaces
export PROJECT_LABEL="Sample Project"
# Remove a variable from the current shell
unset VAR1
Remember the process model: assignments belong to the current shell, exported variables are copied into child processes, and independent terminal sessions do not receive interactive changes automatically. For a broader reference, see Linux environment variables.