Linux Environment Variables: Displaying, Setting, Exporting, and Removing Variables
Learn how Linux environment variables work, how to view, create, export, use, persist, and remove them in a Bash-compatible shell.
Environment variables are named settings supplied to a shell or process as part of its runtime environment. They provide configuration and context without requiring programs to hard-code details that differ between users, machines, or sessions.
For example, a program can consult HOME to find the current user's home directory. It does not need to assume that every user stores files in the same path.
How Environment Variables Work
A process is a running program. When a shell starts another program, the new program is called a child process. The child can receive a copy of the environment from its parent shell.
Variable values can differ by user, shell, session, or system. Two users may have different HOME and USER values. Two terminal windows may also have different custom variables if those variables were created interactively in only one shell.
Per-user environment context
Each user account normally has its own home directory. A typical account might use a path such as /home/alex, while another account might use /home/sam. Applications use $HOME when they need a user-specific location, such as a configuration or data directory.
echo "$HOME"
The dollar sign in this command requests parameter expansion: the shell replaces $HOME with the value stored in HOME before running echo.
Viewing the Current Environment
Use env to list variables exported in the current process environment:
env
The output is a list of name=value entries. You may see entries such as HOME=/home/alex, a long PATH, and SHELL=/bin/bash. The exact output depends on the current user, shell, session, operating system, and programs that started the shell.
Creating Shell Variables
Assign a value with the form NAME=value. There must be no spaces around the equals sign:
VAR1=example
echo "$VAR1"
This creates or changes VAR1 in the current shell. The value can be read with a leading dollar sign, as in $VAR1, but the dollar sign is not part of the variable's name.
A plain assignment creates a shell variable. A shell variable is held by the current shell and is not automatically included in the environment passed to child programs.
Exporting Variables to Child Processes
Exporting marks a shell variable for inheritance by programs launched from that shell. The inheritance direction is from the parent shell to newly started child processes.
Export an existing variable
VAR1=example
export VAR1
env
After export VAR1, a program started by this shell can receive VAR1. The variable should then appear in the output from env.
Assign and export in one command
export VAR1=example
echo "$VAR1"
env
The combined form both assigns the value and exports the variable.
| Feature | Shell-only variable | Exported variable |
|---|---|---|
| Created with assignment | NAME=value | NAME=value, followed by export, or export NAME=value |
| Available in current shell | Yes | Yes |
| Visible to newly launched programs | No, unless separately passed | Yes |
Listed by env | No | Yes |
| Lifetime | Usually the current shell session | The current shell and its descendants, unless changed or removed |
Reading Variable Values
Use parameter expansion to retrieve a value. The common form is a dollar sign followed by the variable name:
echo "$VAR1"
echo "$HOME"
echo "$PATH"
Quoting the expansion, as in echo "$VAR1", is a useful habit because it preserves spaces and prevents unwanted word splitting in many shell commands.
| Task | Syntax pattern | Dollar sign used? | Scope result |
|---|---|---|---|
| Assign | NAME=value | No | Creates or changes a variable in the current shell |
| Export an existing variable | export NAME | No | Makes the variable available to child processes |
| Assign and export | export NAME=value | No | Creates, changes, and exports the variable |
| Read a value | echo "$NAME" | Yes | Substitutes the value in the current shell command |
| Remove a variable | unset NAME | No | Removes it from the current shell |
Common Linux Environment Variables
| Variable | Typical purpose | Example kind of value | Caution or usage note |
|---|---|---|---|
HOME | Path to the current user's home directory | /home/alex | Applications use it for user-specific files; do not casually replace it |
PATH | Directories searched for executable commands | /usr/local/bin:/usr/bin:/bin | Preserve the existing value when adding a directory |
SHELL | User's configured or usual command shell | /bin/bash | It describes a shell path but does not itself change the running shell |
USER or LOGNAME | Current account name | alex | Useful for display and context; do not treat it as a security check |
PWD | Current working directory | /home/alex/projects | The shell updates it as the working directory changes |
LANG | Locale and language preferences | en_US.UTF-8 | Affects formatting, language, and character handling |
TERM | Terminal capability description | xterm-256color | Terminal programs use it to select supported behavior |
PATH deserves particular care. When you type a command without its full path, the shell searches the directories listed in PATH, separated by colons. Replacing PATH carelessly can make ordinary commands appear to be missing.
echo "$PATH"
export PATH="$HOME/bin:$PATH"
This example adds $HOME/bin before the existing directories while preserving the previous value.
Session Scope and Persistence
Variables created interactively apply to the current shell session unless startup configuration is changed. If you run export VAR1=example in one terminal and then open a separate terminal, the new terminal does not automatically receive VAR1 from the first terminal. Each terminal normally starts its own shell and environment.
# Terminal 1
export VAR1=example
echo "$VAR1"
# Terminal 2
echo "$VAR1"
In the second terminal, the expansion may be empty because no value was defined there. A child process inherits from its parent; a separately opened terminal is not a child of the first shell merely because both are visible on the desktop.
Startup files
A startup file is a shell configuration file read when a shell starts. Put recurring per-user settings in the appropriate user startup configuration, commonly ~/.bashrc and/or ~/.bash_profile or ~/.profile. These files affect one account rather than every user.
Global startup configuration, commonly /etc/profile and shell-specific system configuration files, can establish defaults for multiple users. Editing system-wide files normally requires administrator permission and affects more accounts, so use them only when a system-wide setting is intended.
The correct file depends on how the shell starts. A login shell and an interactive non-login shell can read different files. Configure the file that matches the intended use, then open a new appropriate shell session or reread the configuration when suitable.
# Example user setting in a suitable startup file
export VAR1=example
For a shell that supports it, a file can be reread with source, for example source ~/.bashrc. Starting a new shell is often simpler when testing startup behavior.
Removing Variables
Use unset followed by the variable name, without a dollar sign:
unset VAR1
echo "$VAR1"
env
After removal, echo "$VAR1" normally prints an empty line, and VAR1 should no longer appear in env if it was exported. The command is unset VAR1, not unset $VAR1; the latter expands the value and supplies that value as the name to remove.
Complete Practice Workflow
The following sequence demonstrates assignment, reading, exporting, inspection, and removal:
VAR1=example
echo "$VAR1"
export VAR1
env
unset VAR1
echo "$VAR1"
env
VAR1=examplecreates a shell-only variable.echo "$VAR1"reads its value through parameter expansion.export VAR1adds it to the environment inherited by child processes.envdisplays the exported entry.unset VAR1removes the variable from the current shell.- The final checks confirm that the old value is no longer available.
Troubleshooting
A variable works in one terminal but is empty in another
The variable was probably assigned only in the first shell session. Add the assignment or export command to the appropriate user startup file, then start a new matching shell session.
A program cannot see a value that echo displays
The variable was likely assigned but not exported. Run export NAME or use export NAME=value before launching the program.
The shell reports command not found after an assignment
Spaces around the equals sign cause the shell to parse the input as a command and arguments. Use NAME=value, not NAME = value.
unset does not remove the intended variable
Use the name without expansion: unset NAME, not unset $NAME.
Commands disappear after editing PATH
The previous PATH was probably replaced. Restore a valid value and extend it while preserving the old value, for example export PATH="$HOME/bin:$PATH".
A startup-file change is not visible
The current terminal may not have reread the file, or the edited file may not be used by that shell type. Start a new appropriate shell or source the intended file when suitable, and verify the login versus interactive startup behavior.
Exam-Relevant Notes
- An environment variable is a named runtime setting supplied to a shell or process.
NAME=valuecreates or changes a variable in the current shell; spaces around=are invalid for this assignment form.export NAMEmakes an existing shell variable inheritable by child processes.- Use
$NAMEwhen reading a value, but omit$when assigning, exporting by name, or unsetting. envlists exported variables, not every shell-only variable.- Interactive assignments are temporary unless startup configuration establishes them again.
HOMEidentifies the current user's home directory, whilePATHcontrols command lookup.
For more Bash context, see Bourne Again Shell Bash. Related command lookup concepts are covered in Show The Full Path Of Shell Commands.