Environment Variables in Linux: How to List, Set & Manage

December 5, 2024

Introduction

Environment variables in Linux are dynamic values stored in system memory. They influence the behavior of processes and applications and specify user preferences, system paths, and configuration settings.

Every time you start a shell session in Linux, the system goes through the configuration files and sets up the environment accordingly. Environment variables play a significant role in this process.

In this tutorial, you will learn how to set, view, export, and remove environment variables in Linux.

How to set environment variables in Linux - a tutorial.

Prerequisites

What Are Environment Variables?

Environment variables in Linux are key-value pairs that define the operating environment for processes. They provide critical configuration information, such as file paths, user details, and system behavior settings. These variables can be system-wide or user-specific, and they are inherited by child processes.

An example of an environment variable is the PATH variable. It specifies directories where the system searches for executables. To see a variable's value, use the echo command or printenv:

echo $PATH
Printing a variable's value in the terminal with echo.

To create a new variable, use the syntax below:

VARIABLE_NAME=value

If a variable has more than one value, separate them with a semicolon:

VARIABLE_NAME=value_1:value_2 

Use quotation marks for variables that contain spaces:

VARIABLE_NAME="Value text" 

Note: The convention is to use all caps for writing variable names, in order to distinguish them among other configuration options.

Types of Environment Variables

Environment variables in Linux can be classified based on their scope and persistence. These classifications determine who can access the variables, where they are defined, and how long they remain active. The main types are:

  • System Environment Variables. Global variables that apply to all users and processes on the system. They are typically defined in system-wide configuration files like /etc/environment, /etc/profile, or /etc/profile.d/. Changes to these variables require administrative permissions and affect the environment for all users.
  • User Environment Variables. These are specific to individual users and are stored in their profile configuration files, such as ~/.bashrc, ~/.bash_profile, or ~/.zshrc. Changes to user environment variables only affect the respective user's environment and are loaded when the user starts a new session.
  • Session Variables. Temporary variables that exist only for the current shell session. They are not preserved after the session ends unless the user adds them to a configuration file.
  • Persistent Variables. Variables that persist across sessions and reboots. These are typically defined in configuration files like /etc/environment (system-wide) or ~/.bashrc (user-specific) to ensure they are automatically loaded whenever a new session starts.
  • Read-Only Variables. Immutable variables that cannot be modified or unset after they are defined. These are created using the readonly command. The command ensures their values remain constant during the session.

Understanding the different types of environment variables helps manage them effectively for different use cases and access levels.

Common Environment Variables in Linux

There are several predefined environment variables in Linux commonly accessed by users and applications. Below are some of the most frequently used ones:

  • PATH - Specifies directories where the system looks for executable files.
  • PWD - Current working directory.
  • HOME - The user's home directory location.
  • USER - Stores the username of the currently logged-in user.
  • SHELL - The default shell for the user (bash, zsh, etc.).
  • UID - User's unique identifier.
  • HOSTNAME - Device's hostname on the network.
  • LANG - The system's language and localization settings.
  • MAIL - User's mail directory.
  • EDITOR - The Linux system default text editor.
  • TEMP - Directory location for temporary files.

How to List Environment Variables in Linux

Being able to list and view environment variables is essential for troubleshooting issues and system and program customization. Linux offers several methods to list environment variables, allowing users to inspect both global and session-specific settings.

The sections below explore various ways to effectively display environment variables in different contexts.

View All Environment Variables

Use the printenv command to view all environment variables. Since there are many variables on the list, use the less command to control the view:

printenv | less

The output shows the first page of the list and allows you to go further by pressing Space to see the next page or Enter to display the next line:

Using the printenv command to see all environment variables.

Exit the view with q.

Search Specific Environment Variables

There are multiple ways to search for a specific environment variable or variables containing a certain string in their name. Refer to the sections below.

Find Variables with printenv

Use the following syntax to check a single environment variable value:

printenv [VARIABLE_NAME]

The HOME variable value is the home folder path:

Using the printenv command to see the value of the HOME variable.

Find Variables with echo

You can also display a variable's value with the echo command. The syntax is:

echo $[VARIABLE_NAME]

For example:

Printing a variable value with echo.

Search for Variables Using Strings

If you want to find all the variables containing a certain character string, pipe the printenv output to the grep command:

printenv | grep [VARIABLE_NAME/STRING]

The search output for the USER variable shows the following lines:

Using the printenv command with grep to search for variables containing a string.

Find Variables with set

Another command you can use to check environment variables is set. However, this command will also include local variables, as well as shell variables and shell functions.

Find an environment variable in the list that contains all the variables and shell functions by piping the set output into grep:

set | grep [VARIABLE_NAME]
Search for Linux variables using set.

How to Create Environment Variables in Linux

Create environment variables in Linux by defining key-value pairs that store configuration or session data. The sections below show how to create different types of environment variables in Linux.

Create User Environment Variable

The simplest way to create a user environment variable is to type its name in the terminal, followed by its value. The syntax is:

[VARIABLE_NAME]=[variable_value]

For example:

EXAMPLE="Hello World"

In this example, we have created a variable called EXAMPLE with a text value. You can confirm the success with:

set | grep EXAMPLE
Creating a user environment variable in Linux.

However, printenv does not return any output because a variable created in this way is a shell variable, not an exported environment variable.

To verify that it is only a shell variable, start a child shell session by typing bash. Inside the child shell, the variable does not exist.

Using the echo command to search for the EXAMPLE variable now returns no output:

Using echo in a child shell to search for a variable.

You must export the variable to make it accessible to child processes, as shown in the section below.

Create System Environment Variable

System environment variables are globally available to all users and processes. They require administrative privileges to create and are stored in system configuration files.

Follow the steps below to create a system environment variable:

1. Use a text editor such as nano to open the /etc/environment file as sudo:

sudo nano /etc/environment

2. Add the variable name and value at the end of the file. Use the following format:

[VARIABLE_NAME]="value"

For example:

Adding a variable to the /etc/environment config file.

3. Save the file and reload the environment:

source /etc/environment

The variable now persists, and it is available to all users across sessions.

Create Temporary Environment Variable

Temporary variables exist only for the duration of the current shell session. Use the export command to create a temporary environment variable. The syntax is:

export [VARIABLE_NAME]="value"

For example:

export TEMP_VAR="Temporary Value"

The variable is visible with printenv or echo commands during the session.

Creating a temporary environment variable in Linux.

Once you close the terminal, the variable no longer exists.

Create Persistent Environment Variable

Persistent environment variables are retained across sessions and reboots. To create a persistent environment variable, add it to a configuration file:

For user-specific variables, add the export command to ~/.bashrc, ~/.bash_profile, or ~/.zshrc, depending on which shell you are using. We will use bash:

echo 'export VARIABLE_NAME="value"' >> ~/.bashrc

Reload the file to apply the changes:

source ~/.bashrc

For system-wide variables, edit the /etc/environment file as described in the previous section.

How to Export Environment Variables in Linux

Export an environment variable to make it accessible to child processes of the current shell. This process is essential when you want programs or scripts launched from the shell to inherit the variable.

Follow the steps below to export an environment variable:

1. Open the terminal and use the export command to export the variable. The syntax is:

export [VARIABLE_NAME]="value"

For example:

export EXAMPLE="Exported Value"

2. Confirm the export with printenv:

Exporting a variable using the export command.

3. If you open a child shell session now, echo returns the environment variable value:

Using the echo command to search for an environmental variable in a child shell.

The environment variable created this way is temporary and disappears after you exit the current shell session.

How to Remove Environment Variables in Linux

To remove an environment variable, use the unset command. The syntax is:

unset [VARIABLE_NAME]

This command permanently removes variables exported through a terminal command.

Variables stored in Linux configuration files are also removed from the current shell session. However, they are set again upon logging in.

To permanently remove a variable you stored in a configuration file, open the file and remove the line containing the variable definition.

Conclusion

This article showed how to set and unset environmental variables on a Linux system. Knowing how to set environment variables will help you more easily configure software packages in the future.

Next, see how to set environment variables in ZSH or print a variable in Bash.

Was this article helpful?
YesNo
Marko Aleksic
Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.
Next you should read
How to Change Hostname on Ubuntu
July 25, 2024

Hostname is a unique identifier of a computer on a network. This tutorial details ways in which...
Read more
How to Use the hostname Command in Linux
October 8, 2020

The Linux hostname command lets you view your computers domain, hostname, and IP...
Read more
Linux Commands Cheat Sheet: With Examples
November 2, 2023

A list of all the important Linux commands in one place. Find the command you need...
Read more
22 Best Linux Text Editors for Programming & Coding
August 8, 2024

A text editor is an application that lets you type text. All Linux distributions come with...
Read more