How to Set Environment Variables in Linux

By 

Updated on

10 min read

Set and List Environment Variables

When you configure a command-line tool, change the language used by a program, or add a directory to PATH, you are working with environment variables. Each process has its own environment, and programs started from that process inherit a copy of its exported variables.

A shell variable is not passed to child processes unless you export it. This guide explains how to list variables, set environment variables in Bash, pass a variable to one command, and make selected values available in future sessions.

Quick Reference

For a printable quick reference, see the env cheatsheet .

TaskCommand or location
List all environment variablesprintenv or env
Print a specific variableprintenv HOME or printf '%s\n' "$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 a variable persistentAdd it to the startup or environment file for the required scope
Reload a shell filesource ~/.bashrc or source ~/.zshrc

Environment Variables and Shell Variables

Variables have the following format:

txt
KEY=value
KEY="Some other value"
KEY=value1:value2
  • Variable names are case-sensitive. By convention, exported environment variables use uppercase names.
  • There is no space around the equals = symbol in an assignment.
  • Quote values that contain spaces or shell metacharacters.
  • The program that reads a variable defines its value format. For example, PATH uses a colon-separated list, but many variables contain a single value.

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

Environment variables are variables marked for export. They are included in the environment passed to commands and child processes started by the current process.

Shell variables apply only to the current shell instance unless you export them. Each shell, such as Bash or Zsh, also has its own internal variables.

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

  • env - Prints the current environment or runs a program with a modified environment.
  • printenv - Prints all environment variables or the values of specified variables.
  • set - With no arguments, lists shell variables and functions. With arguments, it changes shell options or positional parameters.
  • unset - Removes shell variables or functions.
  • export - Marks a shell variable to be inherited by subsequently executed commands.

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

On this system, the output is:

output
/home/linuxize

Here, HOME points to the current user’s home directory.

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

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

printenv prints the values in the same order as the variable names in the command.

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

Your environment will contain additional variables set by the shell, desktop session, and applications.

Below are some of the most common environment variables:

  • USER - The name of the user associated with the session.
  • HOME - The current user’s home directory.
  • EDITOR - The preferred text editor consulted by many command-line programs.
  • SHELL - The path to the user’s login shell, which is not necessarily the shell currently running.
  • LOGNAME - The user’s login name.
  • PATH - Directories searched for executable files, from left to right.
  • LANG - The default locale used when a more specific locale variable is not set.
  • TERM - The terminal type and capabilities expected by terminal applications.
  • MAIL - The location of the user’s mail file.
  • XDG_SESSION_TYPE - The session type, such as tty, x11, or 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

The beginning of the output may look like this:

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. Quote the expansion so that the shell passes the value as one argument:

Terminal
echo "$BASH_VERSION"
output
5.2.21(1)-release

The exact version depends on the Bash package installed on your system.

Set Environment Variables in Bash

To see the difference between shell and environment variables, start by creating a shell variable named MY_VAR:

Terminal
MY_VAR="Linuxize"

Print its value in the current shell with printf:

Terminal
printf '%s\n' "$MY_VAR"
output
Linuxize

At this point, MY_VAR is not exported. Check the environment with printenv:

Terminal
printenv MY_VAR

The command produces no output and returns status 1 because MY_VAR is only a shell variable.

The export command marks the variable for inheritance by subsequently executed commands:

Terminal
export MY_VAR

Now printenv can read the value:

Terminal
printenv MY_VAR
output
Linuxize

This confirms that MY_VAR is part of the environment exported by the current shell.

A child Bash process inherits the exported variable:

Terminal
bash -c 'printenv MY_VAR'
output
Linuxize

The child prints the same value because it inherited MY_VAR when Bash started it.

You can assign and export a variable in one command:

Terminal
export MY_NEW_VAR="My New Var"

Variables exported this way remain available to the current shell and the processes it starts. A child shell inherits them, but an independent login or terminal session will not recreate them unless you add the assignment to an appropriate startup file.

Set a Variable for a Single Command

Place an assignment before a command to include the variable only in that command’s environment:

Terminal
DEBUG=1 printenv DEBUG
output
1

After printenv exits, this assignment does not add DEBUG to the current shell. This pattern is useful for testing or for commands that need a temporary value.

Unsetting Variables

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

Terminal
unset MY_VAR

In Bash, use the -v conditional test to verify that the variable no longer exists:

Terminal
if [[ -v MY_VAR ]]; then printf 'MY_VAR is set\n'; else printf 'MY_VAR is unset\n'; fi
output
MY_VAR is unset

Unlike printing the value, this test distinguishes an unset variable from a variable whose value is an empty string.

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, print it with a quoted expansion:

Terminal
printf '%s\n' "$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).

Make Environment Variables Persistent

An export command changes the current shell and the processes it starts. To recreate a variable in later sessions, add the assignment to a file read by the shell, login process, or service manager that needs it. No single file supplies variables to every shell, desktop application, and service.

Bash and Zsh Startup Files

For a variable needed in interactive Bash terminals, add the export statement to ~/.bashrc. For example:

~/.bashrcsh
export EDITOR="nano"
export PATH="$HOME/bin:$PATH"

Open a new Bash terminal, or load the edited file into the current shell with the source command :

Terminal
source ~/.bashrc

The source command updates only the current shell. It does not change applications or terminals that are already running.

Bash login shells first read /etc/profile, then read the first available file from ~/.bash_profile, ~/.bash_login, and ~/.profile. A login file can explicitly source ~/.bashrc, but Bash does not do that automatically. See .bashrc vs .bash_profile for the complete loading order.

For Zsh, use ~/.zshrc for interactive shells and ~/.zprofile for login shells.

System-Wide Login Variables

On many Linux distributions, PAM reads /etc/environment when creating login sessions. Use simple assignments without export or other shell commands:

/etc/environmenttxt
JAVA_HOME=/usr/lib/jvm/default-java

Use literal values for portability instead of depending on shell expansion. Changes normally require logging out and back in because /etc/environment is not a shell script that you can reliably source.

For variables that require shell syntax, use /etc/profile or a script under /etc/profile.d/ on distributions that source that directory. For example:

/etc/profile.d/java.shsh
export JAVA_HOME="/usr/lib/jvm/default-java"
export PATH="$JAVA_HOME/bin:$PATH"

These files affect login shells, not every process on the system.

Warning
System-wide environment changes affect multiple users. Prefer a per-user file unless every user needs the variable, and make a backup before editing files under /etc.

Variables for systemd User Services

On systemd-based distributions, files under ~/.config/environment.d/ define variables passed to services started by the systemd user manager. Create a file with a .conf extension and KEY=value assignments:

~/.config/environment.d/60-myapp.conftxt
MYAPP_MODE=production

This setting does not automatically affect unrelated SSH sessions, system services, or applications that are already running. After editing the file, reload the user manager configuration:

Terminal
systemctl --user daemon-reload

Restart the affected user service so that it receives the updated environment. For a system service, set variables in the unit with Environment= or EnvironmentFile= instead.

Troubleshooting

A child command cannot see the variable
An assignment such as MY_VAR=value creates a shell variable. Run export MY_VAR, or assign and export it together with export MY_VAR=value, before starting the child command.

A new terminal does not contain the variable
Confirm which shell is running and whether it is a login shell. Put the assignment in the matching startup file, then open a new terminal or source that file in the current shell.

A GUI application or systemd service ignores the variable
Shell startup files affect processes started by that shell. Configure a systemd service with Environment= or EnvironmentFile=, or use environment.d for services started by the user manager, then restart the application or service.

The shell reports an invalid assignment
Do not add spaces around =. Use MY_VAR="some value", not MY_VAR = "some value". Also omit export in files such as /etc/environment and environment.d/*.conf.

FAQ

How do I check if an environment variable is set?
Use printenv VAR >/dev/null and check the exit status. If you need to distinguish between unset and set-but-empty in Bash, use [[ -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. Bash uses ~/.bashrc for interactive non-login shells and the first available login file such as ~/.bash_profile or ~/.profile. Zsh uses ~/.zshrc for interactive shells and ~/.zprofile for login shells.

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.

Does Linux load .env files automatically?
No. A .env file is an application convention, not a Linux or shell startup file. The application, framework, or a separate loader must read it. If the file contains secrets, keep it out of version control and add it to .gitignore.

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