How to Set Environment Variables in Linux

By 

Updated on

9 min read

Set and List Environment Variables

In Linux and Unix-based systems, environment variables are dynamic named values stored within the system that are used by applications launched in shells or subshells. In simple terms, an environment variable is a variable with a name and an associated value.

Environment variables allow you to customize how the system works and the behavior of the applications on the system. For example, the environment variable can store information about the default text editor or browser, the path to executable files, or the system locale and keyboard layout settings.

In this guide, we will explain how to read and set environment and shell variables.

Quick Reference

For a printable quick reference, see the env cheatsheet .

TaskCommand
List all environment variablesprintenv or env
Print a specific variableprintenv HOME or echo $HOME
Set a shell variableMY_VAR="value"
Set an environment variableexport MY_VAR="value"
Remove a variableunset MY_VAR
Add to PATHexport PATH="$HOME/bin:$PATH"
List all variables and functionsset
Make variable persistentAdd it to your shell startup file such as ~/.bashrc, ~/.zshrc, or ~/.profile
Reload config after changessource ~/.bashrc or source ~/.zshrc

Environment Variables and Shell Variables

Variables have the following format:

txt
KEY=value
KEY="Some other value"
KEY=value1:value2
  • The names of the variables are case-sensitive. By convention, environment variables should have UPPER CASE names.
  • When assigning multiple values to the variable, they must be separated by the colon : character.
  • There is no space around the equals = symbol.

Variables can be classified into two main categories: environment variables and shell variables.

Environment variables are variables that are available system-wide and are inherited by all spawned child processes and shells.

Shell variables are variables that apply only to the current shell instance. Each shell such as zsh and bash, has its own set of internal shell variables.

There are several commands available that allow you to list and set environment variables in Linux:

  • env – The command allows you to run another program in a custom environment without modifying the current one. When used without an argument it will print a list of the current environment variables.
  • printenv – The command prints all or the specified environment variables.
  • set – The command sets or unsets shell variables. When used without an argument it will print a list of all variables including environment and shell variables, and shell functions.
  • unset – The command deletes shell and environment variables.
  • export – The command sets environment variables.

List Environment Variables

The most commonly used command to display environment variables is printenv. If the name of the variable is passed as an argument to the command, only the value of that variable is displayed. If no argument is specified, printenv prints a list of all environment variables, one variable per line.

For example, to display the value of the HOME environment variable you would run:

Terminal
printenv HOME

The output will print the path of the currently logged in user:

output
/home/linuxize

You can also pass more than one argument to the printenv command:

Terminal
printenv LANG PWD
output
en_US.UTF-8
/home/linuxize

If you run the printenv or env command without any arguments, it will show a list of all environment variables:

Terminal
printenv

The output will look something like this:

output
SHELL=/bin/bash
USER=linuxize
HOME=/home/linuxize
LANG=en_US.UTF-8
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
TERM=xterm-256color
XDG_SESSION_TYPE=tty
XDG_RUNTIME_DIR=/run/user/1000
MAIL=/var/mail/linuxize
LOGNAME=linuxize

Below are some of the most common environment variables:

  • USER - The current logged-in user.
  • HOME - The home directory of the current user.
  • EDITOR - The default file editor to be used. Programs such as crontab -e, git commit, and visudo open this editor when they need text input.
  • SHELL - The path of the current user’s shell, such as bash or zsh.
  • LOGNAME - The name of the current user.
  • PATH - A list of directories to be searched when executing commands. When you run a command the system will search those directories in this order and use the first found executable.
  • LANG - The current locales settings.
  • TERM - The current terminal emulation.
  • MAIL - Location of where the current user’s mail is stored.
  • XDG_SESSION_TYPE - The type of session (e.g., tty, x11, wayland).

The printenv and env commands print only the environment variables. If you want to get a list of all variables, including environment, shell variables, and shell functions , you can use the set command:

Terminal
set
output
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote:force_fignore:histappend:interactive_comments:login_shell:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()

The command will display a large list of all variables so you probably want to pipe the output to the less command.

Terminal
set | less

You can also use the echo command to print a shell variable. For example, to print the value of the BASH_VERSION variable you would run:

Terminal
echo $BASH_VERSION
output
5.2.21(1)-release

Setting Environment Variables

To better illustrate the difference between the Shell and Environment variables we’ll start with setting Shell Variables and then move on to the Environment variables.

To create a new shell variable with the name MY_VAR and value Linuxize simply type:

Terminal
MY_VAR='Linuxize'

You can verify that the variable is set by using either echo $MY_VAR or filtering the output of the set command with grep set | grep MY_VAR:

Terminal
echo $MY_VAR
output
Linuxize

Use the printenv command to check whether this variable is an environment variable or not:

Terminal
printenv MY_VAR

The output will be empty which tells us that the variable is not an environment variable.

You can also try to print the variable in a new shell and you will get an empty output.

Terminal
bash -c 'echo $MY_VAR'

The export command is used to set Environment variables.

To create an environment variable simply export the shell variable as an environment variable:

Terminal
export MY_VAR

You can check this by running:

Terminal
printenv MY_VAR
output
Linuxize

If you try to print the variable in a new shell this time you will get the variable value printed on your terminal:

Terminal
bash -c 'echo $MY_VAR'
output
Linuxize

You can also set environment variables in a single line:

Terminal
export MY_NEW_VAR="My New Var"

Environment variables created in this way are available only in the current session. If you open a new shell or if you log out, all variables will be lost.

Set a Variable for a Single Command

You can set a variable for a single command without exporting it globally:

Terminal
DEBUG=1 myapp --verbose

This is useful for testing or for commands that need temporary values.

Unsetting Variables

To remove an environment or shell variable, use the unset command:

Terminal
unset MY_VAR

You can verify that the variable has been removed:

Terminal
echo $MY_VAR

The output will be empty, confirming the variable no longer exists.

Info
The unset command works for both shell and environment variables.

Working with the PATH Variable

The PATH variable is one of the most important environment variables. It specifies a list of directories where the shell looks for executable files.

To view your current PATH:

Terminal
echo $PATH
output
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

Each directory is separated by a colon :. When you type a command, the shell searches these directories from left to right and runs the first matching executable.

To add a new directory to PATH:

Terminal
export PATH="$HOME/bin:$PATH"

This prepends $HOME/bin to the beginning of PATH, so executables in that directory will take priority.

To append a directory to the end of PATH:

Terminal
export PATH="$PATH:/opt/myapp/bin"
Info
Changes made to PATH in the current session are temporary. To make them permanent, add the export line to your shell configuration file (see the next section).

Persistent Environment Variables

To make environment variables persistent, define them in shell startup files. In most Linux distributions, when you start a new session, environment variables are read from the following files:

  • /etc/environment - Use this file to set up system-wide environment variables. This file accepts only simple KEY=value pairs and does not support export, variable expansion, or shell syntax. Variables in this file are set in the following format:

    sh
    FOO=bar
    VAR_TEST="Test Var"
  • /etc/profile - Variables set in this file are loaded whenever a bash login shell is entered. When declaring environment variables in this file, you need to use the export command:

    sh
    export JAVA_HOME="/path/to/java/home"
    export PATH=$PATH:$JAVA_HOME/bin
  • Per-user shell-specific configuration files. For example, if you are using Bash, you can declare the variables in ~/.bashrc. If you are using Zsh, use ~/.zshrc:

    sh
    export PATH="$HOME/bin:$PATH"
  • /etc/profile.d/*.sh - System-wide shell scripts sourced by /etc/profile. This is a common place to drop app-specific export statements.

  • ~/.config/environment.d/*.conf - On systemd-based systems, you can define user environment variables here using KEY=value lines.

Understanding Shell Configuration Files

Different configuration files are loaded depending on how the shell is started:

  • ~/.bash_profile or ~/.profile - Loaded for login shells (when you log in via SSH or a console). This is where you should set environment variables that apply to your entire session.
  • ~/.bashrc - Loaded for interactive non-login shells (when you open a new terminal window). This is where you should set aliases, functions, and shell-specific settings.
  • ~/.zshrc - The equivalent of ~/.bashrc for Zsh users.
Info
On most Linux distributions, ~/.bash_profile sources ~/.bashrc, so variables set in ~/.bashrc are available in login shells too.

To load the new environment variables into the current shell session use the source command:

Terminal
source ~/.bashrc

FAQ

How do I check if an environment variable is set?
Use printenv VAR >/dev/null and check the exit code. If you need to distinguish between unset and set-but-empty in a shell script, use ${VAR+x} or, in Bash, [ -v VAR ].

How do I set an environment variable for a single command?
Use VAR=value command, for example: DEBUG=1 my_command.

Where should I put persistent variables in Bash or Zsh?
Use the startup file that matches how your shell is launched. ~/.bashrc is common for interactive Bash shells, ~/.profile or ~/.bash_profile is used for Bash login shells, and ~/.zshrc is used for Zsh. For system-wide variables, use /etc/profile, /etc/profile.d/*.sh, or /etc/environment depending on the type of variable.

What is the difference between shell and environment variables?
Shell variables exist only in the current shell. Environment variables are exported and inherited by child processes.

Conclusion

Environment variables control how your shell and applications behave. For a deeper look at shell startup files and scripting patterns, see the export command reference.

Tags

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page