How To Set Environment Variables in Linux

In this tutorial, we’ll learn how to set environment variables in Linux manually. When using Linux, the Kernel sets up the environment with default values for a lot of variables that we would need to use. This includes the default paths, languages, default shell, home directory, and more. Let’s go over how and where Linux stores these variables, and how we can manage them, edit them and work with them.

Where Are Environment Variables Located in Linux?

The environment variables are not stored in some file but exist when a process is run. The variables are stored in virtual memory of the process and in a virtual file /proc/<ProcessID>/environ.

This file shows all the environment variables that were passed when calling the process. The kernel makes those virtual environments visible through this virtual file.

Let’s run the ps command and see what processes are actively running.

root@HowLinux:~# ps
  PID TTY          TIME CMD
26513 pts/0    00:00:00 bash
27943 pts/0    00:00:00 bash
28024 pts/0    00:00:00 ps

As you can see we have two bash processes and the ps process that we just run. We’ll replace the <ProcessID> in the path above to see what default environment variables are being used for the bash process. The ProcessID is in the first column (PID – 26513)

cat /proc/26513/environ | tr '\0' '\n'
Bash Environment Variables Output
Bash Environment Variables Output

To make this output look a little better, we can replace the default delimiter 0 with a new line (\n) with the help of tr

cat /proc/26513/environ | tr '\0' '\n'
Bash Environment Variables Output Prettified
Bash Environment Variables Output Prettified

Commands to List Environment Variables

You can list out the Global and Local environment variables with the use of three commands.

  • printenv
  • env
  • set

The env and printenv commands, when used without options will simply output the same set of environment variables that are active. Both commands have access to the environment variables that the currently active shell passes on to them.

Printenv Output
Printenv Command Output
Env Output
Env Command Output

The set command is built-in to the shell and can see the shell-local variables including the shell functions. So you will end up seeing a lot more environment variables with the set command than with the printenv or env commands.

Set Command Output
Set Command Output

Set Environment Variables in Linux

Now that we know where the environment variables are, and what they are, let’s learn to modify, set environment variables in Linux or add new data to the existing environment variables. The most common use for environment variables with programmers is to set up default paths. Let’s begin with that first.

1. How to Add or Change the PATH environment variable

We will export the environment variable by using the “export” command that will let us set up the variable in the temporary memory.

root@HowLinux:~# echo $PATH
Output Path Environment Variable
Output Path Environment Variable

If we want to add a new variable to the default PATHs, we will use the export command. But remember, the export command will overwrite the existing paths. So we need to also add the $PATH variable while to avoid losing existing defaults. Let’s see an example of what happens if we do a direct export with the path we want to add.

root@HowLinux:~# export PATH=/home/
root@HowLinux:~# echo $PATH

The above will output the new PATH variable which only has the /home/ as it’s default paths.

Export Path Variable Overwrite
Export Path Variable Overwrite

To avoid this, we’ll add the $PATH variable while exporting.

root@HowLinux:~# export PATH=$PATH:/home/
root@HowLinux:~# echo $PATH
Export Path Variable Without Overwrite
Export Path Variable Without Overwrite

As you can see, here the PATH variable wasn’t overwritten because we also passed the $PATH to the export command. This is more frequently used with the PATH command but not limited to it. You can set and create your own variables too.

root@HowLinux:~# printenv | grep HowLinux

root@HowLinux:~# export HowLinux="Everything you need to learn about Linux"

root@HowLinux:~# printenv | grep HowLinux
HowLinux=Everything you need to learn about Linux
Create Own Environment Variable
Create Own Environment Variable

When we first output the printenv command and tried to look for HowLinux using grep, we found nothing. But after exporting, we can see the environment variable has been added to the list.

2. How To Set Environment Variables in Linux To Stay After Reboot

Depending on how you want to set environment variables in Linux, you can add the variables to one of the files below.


  1. /etc/environment
  2. /etc/profile and /etc/profile.d/*
  3. /etc/bash.bashrc


  1. ~/.pam_environment
  2. ~/.xprofile
  3. ~/.profile, ~/.bash_profile, ~/.bash_login
  4. ~/.bashrc

For example, if you want a PATH to be accessible system-wide irrespective of who is logged in, you can add the path to the /etc/environment file. If you want the scope to be limited to the current user, you can go with ~/.profile. You can also add variables to shell specific files like the ~/.bashrc which will limit the scope of a variable to the user AND the shell. The /etc/bash.bashrc file in Ubuntu will limit the scope to the shell irrespective of the user.

3. How to Add Variables and Instantly Update Them For Current Session

When using the export command, the environment variables show the effect immediately. But when you set environment variables in Linux to be persistent, the file does not get executed so the variables do not exist in the system until the file is run.

The file is run every time you log in or restart your system. But what if you do not want to restart or even log out of your system? This can be achieved with the use of the source command in Linux.

For this tutorial, let’s set up a sample variable in the ~/.profile file. You can create this file if you do not already have it. In my case, I don’t have this file existing in my home directory so I’ll use the touch command to create it.

root@HowLinux:~# touch ~/.profile

I’ll create a sample environment variable named Tutorial in the ~/.profile file. Add a line Tutorial='test' in the .profile file. Once you’ve added the line and saved the file, run the source command as below.

root@HowLinux:~# source ~/.profile
Source Command Update Environment Variables 1
Source Command Update Environment Variables 1


If you’ve followed through, you should have a perfect understanding of how variables work, where they’re located and how you can manage, modify, add and set environment variables in Linux. And yes, when in doubt, the man pages will explain everything that a command can do.