Wondering how to set environment variables in Linux? This is exactly what we’ll be doing in this tutorial. Operating systems make use of environment variables to define system properties for the runtime environment.
Through this tutorial, we will help you understand how environment variables work. Further, we will see how to set environment variables in Linux or reset existing environment variables as per our needs.
Understanding Environment Variables
When we start a session on our server through the shell, it has to create an environment for us. This is a mandatory step for us to interact with the server through the shell.
Environment variables are special variables that are available to all commands and processes on a system. These variables have the following formats.
1 2 3 |
ENVAR=value ENVAR="Another value" ENVAR=value_1:value_2 |
- All variable names are case-sensitive. We provide all environment variables with an uppercase name by convention.
- When assigning a value to our variable, we make sure there is no space around the assignment operator (=).
- If we need to assign multiple values to a single environment variable, we make use of a colon (:) to separate the different values.
List existing Environment Variables in Linux
If we want to see the environment variables for our current session, we use the following command.
1 |
printenv |

As seen in the screenshot above, this command will print all the variables for the current shell session. If we want a list of shell variables with environment variables, we use this command.
1 |
set |
This will return a similar output displaying the shell variables for the current shell session.
Below are some examples of environment variables with their importance.
Type of Variable | Significance |
SHELL | The shell which interprets all the commands entered by the user. Usually, this will be set to bash by default. But users can change it as per their to suit their needs. |
USER | The username of the user currently logged on to the system. |
LANG | The language and localization settings for the system currently in place, which includes character encoding. |
TERM | The type of terminal which needs to be emulated when the shell is running. While this isn’t something you would need to usually worry about, there can be instances when your operating needs might require emulating a specific hardware terminal. |
HOME | The home directory for the user currently logged on to the system. |
PATH | The list of directories in which our system will check when a command is entered by the user. This search of directories is done to find the executable files associated with the command. |
PWD | The current directory which the user is working with. |
How to set environment variables in Linux?
Now that we know what environment variables are, it is time to learn how to set environment variables in Linux.
1. Create a shell variable and assign a value
When we need to create a new environment variable, we first need to create a variable and initialize it. For this tutorial, we will use the following.
1 2 |
TESTVAR="This is a test variable" echo $TESTVAR |
We’ve used the echo command to display the variable and confirm its existence.
Now, if you run the printenv command with the variable name as shown below, you will not get any output since the variable is currently a shell variable
1 |
printenv TESTVAR |
The above command will give us no output because TESTVAR isn’t an environment variable yet.
2. Export to Convert it to an Environment Variable
Let’s make it into an environment variable using the export command and then we will use the printenv command once again.
1 2 |
export TESTVAR printenv TESTVAR |

As you can see above, we get the value stored by TESTVAR. This is because by using the export command, we made TESTVAR an environment variable.
3. Alternative – Combining the two steps
There is a more commonly used method when you want to set environment variables in Linux. We can use the below line to declare and export the variable at the same time.
1 2 |
export NEWTESTVAR="This is a new test variable" printenv NEWTESTVAR |

Now when we use the printenv command for our new variable, we will see that it displays the environment variable right away.
How to delete environment variables in Linux
Now that we know how to set environment variables in Linux, we need to learn how to remove them. Once we have no use for our created environment variables, it is advised to delete them.
This ensures that the environment variable isn’t called unintentionally.
1. Using the export Command
1 2 |
export -n TESTVAR printenv TESTVAR |
Now, if we try using the printenv command to get the value of TESTVAR, we will get no output.
This confirms that TESTVAR is no longer an environment variable.
2. Using the unset Command
Alternatively, we can use the unset command. Using it will remove the variable completely so that it is neither an environment variable nor a shell variable.
This can be done as given below.
1 |
unset TESTVAR |

Notice that nothing is returned even when we use the echo command. Thus, we know that the variable is unset.
Conclusion
With environment variables having so much control over how our shell session functions, it is important for any shell user to understand how to set environment variables in Linux.
Manipulating or creating new variables can greatly assist when you wish to customize your shell session and make your tasks easier.