A variable is a location for storing a value which could be a filename, text, number or any other data.

A variable is usually referred to with its symbolic name. The value stored can be displayed, deleted, edited, and re-saved. Variables play an important role in computer programming because they enable programmers to write flexible programs.

An environment variable alters the dynamic values which affect the processes or programs on a computer. They exist in every operating system and their types vary. Environment variables also give information about system behavior and can change the behavior of software and programs.

In this tutorial, we will show you how to set environment variables in Linux.

Step 1 – List Environment Variables

You can list all environment variables in your system by running the following command:

printenv

Or:

env

You should see the following screen:

There are a lot of environment variables available in Linux, including HOME, USER, EDITOR, PATH, LANG, TERM, MAIL, SHELL, and many more.

You can display the value of specific variable by passing the name of the variable to the printenv command.

For example, to display the value of the LANG variable, use the following command:

printenv LANG

You should see the following output:

en_US.UTF-8

Step 2 – Set up an Environment Variable

There are two types of variables: Shell variables and the Environment variables.

To create a new shell variable named COMPANY with value Atlantic, use the following command:

COMPANY="Atlantic"

Next, you can verify the variable using the echo command:

echo $COMPANY

You should see the value of your variable in the following output:

Atlantic

You can also verify whether the above variable is an environment variable or not using the printenv command:

printenv COMPANY

You should see the empty output which means the variable is not an environment variable.

To create a new environment variable named ORGANIZATION, use the export command:

export ORGANIZATION=Atlantic

You can verify the environment variable with the following command:

printenv ORGANIZATION

You should get the following output:

Atlantic

The environment variables which you have created in this way are available only in the current session. If you log out of your system, all variables will be lost.

You can also unset an environment variable you created earlier using the unset command followed by the variable name.

For example, to unset an ORGANIZATION variable, use the following command:

unset ORGANIZATION

Step 3 – Set up Persisting Environment Variable

When you set an environment variable using the export command, its existence ends when the user’s session ends, so it is recommended to set environment variables that are persist across sessions.

To set persisting environment variables globally, you will need to define them in /etc/profile file.

For example, you can set the persisting environment variable named COMPANY with value Atlantic by editing /etc/profile file as shown below:

nano /etc/profile

Add the following line at the end of the file:

export COMPANY=Atlantic

Save and close the file when you are finished, then activate the variable with the following command:

source /etc/profile

You can also set a persisting environment for a specific user by editing the user’s .bashrc file.

For example, set a persisting environment named VAR with value “My Linux Box” by editing the user’s .bashrc file:

nano ~/.bashrc

Add the following line at the end of the file:

export VAR="My Linux Box"

Save and close the file when you are finished, then activate the environment variable with the following command:

source ~/.bashrc

Conclusion

In the above guide, you learned how to set and unset an environment variable in Linux. Environment variables are very useful for developers who will need to read or alter the environment of your system. If you’re ready to try setting an environment variable in Linux, get started on dedicated server hosting from Atlantic.Net.