How to Set an Environment Variable in Linux

In programming, variables are used to temporarily store information such as strings and numbers.

Variables can be used multiple times throughout the code or by the operating system to store values. You can modify, overwrite, or delete them as needed.

In this tutorial, I’ll explain what environment variables are and show you how to set them in Linux.

What Are Environment Variables?

Environment variables are specific to a particular environment. For instance, each user in an operating system has a distinct environment. An administrator user, for example, has a different environment compared to standard users.

You may define an environment variable that is specific to your user, such as a secret token, which doesn’t need to be shared with other users.

Here are a few examples of environment variables in Linux:

  • USER – This indicates the currently logged-in user.
  • HOME – This represents the home directory of the current user.
  • SHELL – This contains the path to the current user’s shell, such as bash or zsh.
  • LANG – This variable specifies the current language and locale settings.
  • MAIL – This indicates the location where the current user’s mail is stored.

These environment variables change depending on the current user session.

How to List Environment Variables in Linux

The command to display all environment variables defined for the current session is env.

Here is the output for my session:

root@Zaira:~# env
SHELL=/bin/bash
PWD=/root
LOGNAME=root
HOME=/root
LANG=C.UTF-8
LESSOPEN=| /usr/bin/lesspipe %s
USER=root
SHLVL=1
XDG_DATA_DIRS=/usr/local/share:/usr/share:/var/lib/snapd/desktop
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
MAIL=/var/mail/root
_=/usr/bin/env

How to Print Environment Variables in Linux

There are two methods to print the environment variables that are already defined:

  • printenv VARIABLE_NAME
  • echo $varname

Let’s print the value of the SHELL variable using both methods. Here’s an example using printenv:

root@Zaira:~# printenv SHELL
/bin/bash

Here’s an example using echo:

root@Zaira:~# echo $SHELL
/bin/bash

How to Set Environment Variables in Linux

The basic syntax for defining an environment variable is as follows:

export VARIABLE_NAME=value

Let’s create an environment variable, display it, and print its value.

  • Set the variable JAVA_HOME:
root@Zaira:~# export JAVA_HOME=/usr/bin/java
  • Check by listing it:
root@Zaira:~# env
SHELL=/bin/bash
JAVA_HOME=/usr/bin/java
PWD=/root
LOGNAME=root
HOME=/root
LANG=C.UTF-8
LESSCLOSE=/usr/bin/lesspipe %s %s
TERM=xterm-256color
global22=yolo
LESSOPEN=| /usr/bin/lesspipe %s
USER=root
SHLVL=1
XDG_DATA_DIRS=/usr/local/share:/usr/share:/var/lib/snapd/desktop
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
MAIL=/var/mail/root
_=/usr/bin/env
  • Display its value:
root@Zaira:~# echo $JAVA_HOME
/usr/bin/java

However, variables defined using this method are only stored for the current session and won’t be available in the next session.

Let’s confirm by opening a new session and displaying the variable’s value.

zaira@Zaira:/etc$ echo $JAVA_HOME

However, we can make the definitions persistent, as explained in the next section.

How to Make Environment Variables Persistent in Linux

To make the JAVA_HOME variable persistent, edit the .bashrc file and set its value there.

The .bashrc file is a script that runs each time a user logs in. It is hidden and typically found in the user’s home directory.

I have modified my .bashrc file as shown below:

vi ~/.bashrc

ImageAdd the definition of the environment variable at the end of the .bashrc file

To apply the changes, update the .bashrc file using the source command:

source .bashrc

Let’s confirm by starting a new session.

root@Zaira:~# echo $JAVA_HOME
/usr/bin/java

How to Create a Persistent Global Variable in Linux

There may be times when you need to define a global environment variable that is accessible to all users.

To do this, we first need to declare the variable and modify the appropriate files where environment variables are sourced from.

Let’s go step by step.

  1. I am logged in as the user Zaira and am creating a global variable called GLOBAL_VARIABLE as follows:
zaira@Zaira:~$ export GLOBAL_VARIABLE="This is a global variable"
  1. Edit the following file:
  2. /etc/environment – This file is used to configure system-wide environment variables.

ImageUpdate the /etc/environment file

For the changes to take effect, use the command source /etc/environment.

  • /etc/profile – Variables set in this file are read whenever a bash shell is logged in. Edit this file and use the export command:

ImageUpdate the /etc/profile

Time to test!

Now, I’ll switch to the root user and check if I can access the GLOBAL_VARIABLE.

root@Zaira:~# echo $GLOBAL_VARIABLE
This is a global variable

It worked! I was able to access the global variable defined by the user Zaira from the root user as well. The same applies to other users too. Now you also know how to define global environment variables.

Conclusion

In this tutorial, you learned how to create and define environment variables in Linux, as well as how to make them persistent so they can be used across multiple sessions.

Leave a Comment