1. Home
  2. Linux

How to set Linux environment variable

If you’re installing software on your Linux PC that requires modifying or adding environmental variables, knowing how to set a Linux environment variable is critical. This guide will help you understand what a Linux environment variable is and how to set them.

What is an environment variable?

An environment variable is a variable used on the Linux platform to set up a shell environment. They act like configurations in a configuration file for Linux and can change how programs on the system function. Environmental variables are persistent and stay set unless changed manually. 

The average Linux user will never need to touch environment variables on their Linux system. However, sometimes, variables need to be added, especially if you’re programming, manually compiling software, etc. 

Viewing default environment variables on Linux

By default, every Linux user has a large number of environment variables already set. These variables define how the Linux operating system will function. In this section of the guide, we will show you how to view the default environment variables.

To start, you must open up a terminal window. Sadly, environment variables cannot be modified or viewed outside of the terminal very efficiently. To open up a terminal window, you will need to press the Ctrl + Alt + T keyboard combination or search for “Terminal” in your app menu.

Once the terminal window is open, we can go over the various commands that can be used to look at environment variables on the system. These commands are env and printenv.

The env command is useful for viewing the default variables. When run, it will list every single environment variable in the terminal shell. To view the environment variables in your currently terminal shell, run:


To save the entire output of the env command, pipe it to a text file with the following command.

env > ~/env-output.txt

The second command to view environmental variables on Linux is the printenv command. This command is a little different from the env command. The printenv command will print all defined environment variables in the current environment (current session), while the env command prints every variable.

To view the environment variables in this current session on your Linux system, you can execute the printenv command below.


Want to save the output of the printenv command to a text file for later? Pipe it to a file with the command below.

printenv > ~/printenv-output.txt

Searching for specific environment variables on Linux

While it is useful to be able to view the entire environment variable list in one go, it’s a lot of information to take in at once. If you’re searching for a single variable, the full view isn’t the way to go.

If you’d like to search for a specific environment variable using the env or printenv command, it is possible. How? By piping it through the Grep tool. 

What is Grep? Grep is a searching program for the command-line. Users can use it to search through various files for a specific string or query. Using it in combination with the printenv or env commands allows you to do filtering.

For example, to search for the SHELL environment variable in the long list of environmental variables on your Linux system, combine the env command with the grep command and the search term “SHELL.”

env | grep "SHELL"

After combining env and grep, you’ll see just the “SHELL” environment variable printed on the screen instead of the entire list of variables on the computer. 

To search for any specific environment variable, enter the following command below and modify “VARIABLE” below.

env | grep "VARIABLE"


printenv | grep "VARIABLE"

Set Linux environment variable

Setting environment variables on the Linux operating system is done by editing the .bashrc file for the current user. To start the process, open up the .bashrc file in the Nano text editor using the command below.

nano ~/.bashrc

After the .bashrc file is open in the Nano text editor, go to the bottom of the file and create a new line using the Enter key on the keyboard. Once on a new line, write in the following code. Please note that you will need to change “MY_ENVIRONMENT_VARIABLE” and “MY_ENVIRONMENT_VARIABLE_VALUE” for the variable to work.


With the new code added to the file, it is time to save the edits. Using the keyboard, press the Ctrl + O keyboard combination. Then, press the Ctrl + X keyboard combination to close the Nano text editor with everything saved.

Once the Nano text editor is closed, type in the source command below to start using your new variable.

source ~/.bashrc

Alternatively, close the terminal session and re-open a new terminal window. When the terminal window is open, your Linux system should be using the new environment variable.

Feel free to repeat this process to add in as many custom environment variables as necessary. 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.