How to Set Environment Variables in Linux

Filed Under: Java
How to Set Environment Variables in Linux

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.

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.

printenv
Print Environment Variables In Linux Systems
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.

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 VariableSignificance
SHELLThe 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.
USERThe username of the user currently logged on to the system.
LANGThe language and localization settings for the system currently in place, which includes character encoding.
TERMThe 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.
HOMEThe home directory for the user currently logged on to the system.
PATHThe 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.
PWDThe 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.

TESTVAR="This is a test variable"
echo $TESTVAR

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

Set A Variable In Shell
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

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.

export TESTVAR
printenv TESTVAR
Set An Environment Variable In Linux
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.

export NEWTESTVAR="This is a new test variable"
printenv NEWTESTVAR
One Line Set Environment Variables In Linux 1
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

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.

unset TESTVAR
Unset Environment Variables
Unset Environment Variables

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.

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages