How to set evironment variable in Linux?


Gaming Laptop Series

An environment variable is a variable whose value is set outside the program, typically through a functionality built into the operating system. Usually, environment variables contains information such as drive, path, or filename.

The environment variables can be set, recalled, and cleared with exactly the same syntax used for normal variables but we don’t often use these variables directly instead they are referenced by individual applications and daemons as needed.

In this article, we will discuss how to display, set and unset environment variables in Linux.

Examples of Linux environment variables

The most basic example of this is when you log in to your system your home directory is set as an environment variable you can see this by using the given command.

echo $HOME

This will display the value stored in the HOME environment variable –

Some most common environment variables used in Linux are given below.

SHELL – Default shell such as bash, zsh, c shell, etc

UID – User id associated with the current user

HOME – Home directory of the current user

PWD – Current working directory

TEMP – Directory location for temporary files

EDITOR – Default text editor on Linux

How to display the environment variables on a Linux system

By using printenv command on a Linux system you can view all or information of a specific environment variable.

Use the given command to view all the environment variables and their value on your system –

printenv | more

Use the enter key to scroll the output displayed in the terminal.

Now to display the value of a specific environment variable on Linux, let us say USERNAME then use –

printenv USERNAME

This will display the current username –

You can also use the echo command to display the value stored in an environment variable.

How to set environment variable in Linux

In a Linux system, setting environment variables is not so difficult. By using a few commands or editing files we can easily set their value. Let us understand this by using an example.

EXAMPLE_VAR= 'explorelinux'

Here we assigned a value to EXAMPLE_VAR variable. Now if you display its value using printenv command you will see nothing because this variable is a shell variable, not an environment variable.

printenv EXAMPLE_VAR

See the output below –

To turn this shell variable into an environment variable you need to use the export command. Look at the command below –

export EXAMPLE_VAR

Now EXAMPLE_VAR becomes an environment variable if you use printenv command you will see the value assigned to this variable.

This is temporary when you exit from the shell this will disappear.

To make changes persistent you need to follow a few additional steps.

Set an environment variable permanent in Linux

You can set an environment variable permanent for the current user only or for all users by following the given steps.

For current user

To set an environment variable permanent for a user you need to edit .bashrc file. Use a text editor to edit it –

sudo nano .bashrc

At the bottom of this file add the following line –

export EXAMPLE_VAR= 'explorelinux'

Similarly, you can add each line for a variable that you wish to add. Save this file by pressing ctrl+s and then ctrl+x to exit from it in the nano text editor. Restart your shell to make changes effective.

You can also use the source command to apply the changes during the current session –

source ~/.bashrc

For all users

You can create a permanent environment variable that persists after a reboot by adding it to the default profile. This profile is loaded by all users on the system, including service accounts. All global profile settings are stored under /etc/profile.

To set environment variable permanent for all user create a .sh file in /etc/profile.d/ directory –

For example –

sudo nano /etc/profile.d/file_env.sh

Here add the given line, you can add multiple variables one in each line.

export EXAMPLE_VAR= 'explorelinux'

Save the file and exit. The change will be applied on the next boot.

How to unset an environment variable

To unset the variable you can use the unset command for example to unset EXAMPLE_VAR we will use –

unset EXAMPLE_VAR

On the next login, this value will again set to that variable. To permanently unset the variable edit the file where you add the line to set an environment variable permanently.

Conclusion

Now you know how to set or unset an environment variable on a Linux system. If you have a query then write us in the comments below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.