Linux Applications

How to Set Environment Variables in Linux

The environment variables are dynamic entitled values used for the customization of a Linux system in Linux. It enables you to modify the way your system processes along with the conduct of its applications. For an instance, the environment variables are used to store paths, save information regarding different editors or web browsers, and let us know about the different layout settings. You can take a look at all the current working environment variables in your system and modify or create the variables in it. This article is all about setting the environment variables in Ubuntu 20.04 Linux system using its shell.

Display the Environment Variables

We start with the display of presently enabled environment variables of our Ubuntu 20.04 Linux operating system. The most commonly used command for this purpose is the “printenv” instruction on the shell. We start the terminal with the consummation of the shortcut key “Ctr+Alt+T” and simply perform the “printenv” one-word instruction within it. This command execution displays all the already known environment variables.

Using the printenv keyword in the shell is displaying all the present environment variables, but you can use the name of a specific variable in the same instruction to display the values. For this, you need to use the “VAR_NAME” keyword along with the “=” sign and the name of a variable to be searched within the printenv instruction. We tried the “printenv” instruction along with the “VAR_NAME” specification to use the names of three different variables to see their values, i.e. SHELL, DESKTOP_Session, and PWD currently used shell format, desktop session, and current working directory.

You can also specify the environment variable to be searched without the use of the “VAR_NAME” keyword in the printenv command. The very same output will display.

You must use the grep instruction to search some strings using the specific patterns. It can be merged with the printenv instruction to search all the environment variables containing some specific pattern within their name. We got a minimum of 2 records for each.

The “env” instruction can also be used to display all the environment variables on the Ubuntu 20.04 console. The use of “env” instruction and its output is displayed in the image:

For an instance, if you want to use the “env” instruction on the shell to simply display all the variables on the shell, you need to be clear that it will be a large output, i.e. most is hidden.

The use of the “more” keyword with the “|” sign allows you use the Enter key to display the remaining variables on the console screen.

The grep instruction can also be used with the “env” command to search for the specific pattern environment variables in the Ubuntu 20.04 system as presented.

If any one of you wants to exhibit all the environment variables along with the console, user-defined, and environment methods in the shell terminal of the Ubuntu 20.04 system, one should utilize the set instruction on the query area as follows:

The grep instruction can be utilized in the set instruction as well to display all the environment variables, user-defined methods, and shell variables with a specific pattern in their name.

Set the Environment Variables

Before jumping into setting the environment variables, you need to understand that the shell variables are mainly used to set as environment variables. First, initialize a shell variable and then make it an environment variable. To create a shell variable, we need to use any unique name followed by the equal sign and the variable value in the single inverted commas. So, within the following example, we ceate a new shell variable named “VAL” and initialize it with the “test” value. The “echo” statement is consumed here to display the value of shell variable “VAL” in the console and get the “test” value as an output.

The printenv and env instructions are utilized here to display the value of the shell variable “VAL” just created. The output is either nothing or some error. It is because the printenv and env instructions are only utilized for the display of environment variables and not for the display of shell variables. VAL is currently a shell variable.

Now, it’s time to transform a shell variable to an environment variable. For this, Ubuntu 20.04 Linux operating system came up with the “export” command. You need to use the keyword “export” with the variable name “VAL” as we did in the following. The printenv instruction along with the use of the variable name can be used to check for the result, if the shell variable is set to the environment variable or not. The output displays the “test” value of the “VAL” variable. The shell variable “VAL” is successfully converted to an environment variable.

The Ubuntu users can also create and set an environment variable directly without creating a shell variable first and then converting it to an environment variable. For this, you just have to use the export instruction along with the name of a new environment variable to be created and initialize it with the value. We tried this method to create an environment variable “VAR” with the “SaeedRaza” value and displayed its value using the printenv command.

One can also unset the already set environment variable in the Linux system using the unset instruction along with the variable name. We have done it so far for the newly created environment variables, i.e. VAR and VAL. The unsetting causes the printenv command to display nothing in return.


This article’s first paragraph is the introduction to environment variables and their usage in the Linux system. We divided this tutorial into two main parts: displaying environment variables and setting-up the environment variables. The first part is all about how to display the different variables using the different printenv and env instructions while the second part is about the creation of the environment variables directly and indirectly (using the export command).

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.