How to Set Environment Variables in Linux

December 17, 2020

Introduction

Every time you start a shell session in Linux, the system goes through 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, and unset environment variables in Linux.

How to Set Environment Variables in Linux

Prerequisites

What Are Environment Variables: A Definition

Environment variables are variables that contain values necessary to set up a shell environment. Contrary to shell variables, environment variables persist in the shell’s child processes.

Structurally, environment and shell variables are the same – both are a key-value pair, separated by an equal sign.

VARIABLE_NAME=value

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

VARIABLE_NAME=value_1:value_2 

Variables that contain spaces are written under quotation marks:

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.

Most Common Environment Variables

Here are some environment variables that an average user may encounter:

  • PWD – Current working directory.
  • HOME – The user’s home directory location.
  • SHELL – Current shell (bash, zsh, etc.).
  • LOGNAME – Name of the user.
  • UID – User’s unique identifier.
  • HOSTNAME – Device’s hostname on the network.
  • MAIL – User’s mail directory.
  • EDITOR – The Linux system default text editor.
  • TEMP – Directory location for temporary files.

How to Check Environment Variables

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 will show the first page of the list and then allow 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 by pressing Q.

Search a Single Environment Variable

To check a single environment variable value, use the following command:

printenv VARIABLE NAME

The HOME variable value is the home folder path:

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


Alternatively, display the value of a variable by using the echo command. The syntax is:

echo $[VARIABLE_NAME]

Search Specific Environment Variables

To find all the variables containing a certain character string, use the grep command:

printenv | grep [VARIABLE_NAME]

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

Using the printenv command with grep to look for the USER variable

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 using set | grep:

set | grep [VARIABLE_NAME]

Note: Take a look at our in-depth guide on how to use the Linux set command to learn more about it.

Set an Environment Variable in Linux

The simplest way to set a variable using the command line is to type its name followed by a value:

[VARIABLE_NAME]=[variable_value]

1. As an example, create a variable called EXAMPLE with a text value. If you type the command correctly, the shell does not provide any output.

2. The set | grep command confirms the creation of the variable. However, printenv does not return any output.

Setting a shell variable

This is because the variable created in this way is a shell variable.

3. Another way to confirm this is to type bash and start a child shell session. 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

Note: In a child process, EXAMPLE is not an existing variable.

How to Export an Environment Variable

1. If you want to turn a shell variable into an environment variable, return to the parent shell and export it with the Linux export command:

export [VARIABLE_NAME]

2. Use printenv to confirm the successful export:

Exporting a variable using the export command

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

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

The environment variable created in this way disappears after you exit the current shell session.

Set an Environment Variable in Linux Permanently

If you wish a variable to persist after you close the shell session, you need to set it as an environmental variable permanently. You can choose between setting it for the current user or all users.

1. To set permanent environment variables for a single user, edit the .bashrc file:

sudo nano ~/.bashrc

2. Write a line for each variable you wish to add using the following syntax:

export [VARIABLE_NAME]=[variable_value]
Editing the .bashrc file in nano

3. Save and exit the file. The changes are applied after you restart the shell. If you want to apply the changes during the current session, use the source command:

source ~/.bashrc

4. To set permanent environment variables for all users, create an .sh file in the /etc/profile.d folder:

sudo nano /etc/profile.d/[filename].sh

5. The syntax to add variables to the file is the same as with .bashrc:

Editing the file in the /etc/profile.d folder in nano

6. Save and exit the file. The changes are applied at the next logging in.

How to Unset an Environment Variable

To unset an environment variable, use the unset command:

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 next logging in.

To permanently unset a variable you stored in a file, go to the file, and remove the line containing the variable definition.

Conclusion

After reading this article, you should know 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.

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 20.04
November 25, 2020

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
September 3, 2019

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