How to Set Environment Variables in Linux With Examples

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.

  • 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.

Print Environment Variables In Linux Systems

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.

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.

We’ve used the echo command to display the variable and confirm its existence.


Set A Variable In Shell

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

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.

Set An Environment Variable In Linux

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.

One Line Set Environment Variables In Linux 1

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

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.

Unset Environment Variables

Notice that nothing is returned even when we use the echo command. Thus, we know that the variable is unset.


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.

By admin

Leave a Reply

%d bloggers like this: