In programming, you use variables to store information like strings and numbers temporarily.
Variables can be used repeatedly throughout the code or by your operating system to provide values. You can edit them, overwrite them, and delete them.
In this tutorial, I'll teach you what environment variables are and how to set them in Linux.
What Are Environment Variables?
Environment variables are the variables specific to a certain environment. For example, each user in an operating system has its own environment. An admin user has a different environment than other users do, for example.
You might declare an environment variable only required by your user (for example a secret token) that doesn't need to be exposed to other users.
Here are some examples of environment variables in Linux:
USER– This points to the currently logged-in user.
HOME– This shows the home directory of the current user.
SHELL– This stores the path of the current user’s shell, such as bash or zsh.
LANG– This variable points to the current language/locales settings.
These environment variables vary based on the current user session.
How to List Environment Variables in Linux
The command used to display all the environment variables defined for a current session is
Here is the output for my session:
How to Print Environment Variables in Linux
There are two ways to print the already defined environment variables:
Let's print the value of the variable
SHELL using both methods. Here's an example of printing using
And here's an example of using
How to Set Environment Variables in Linux
The basic syntax to define an environment variable is as follows:
Let's define an environment variable, list it, and print its value.
- Define the variable
root@Zaira:~# export JAVA_HOME=/usr/bin/java
- Verify by listing it:
- Print its value:
root@Zaira:~# echo $JAVA_HOME /usr/bin/java
However, the variables defined using this method are stored for the current session only. They won't be available for the next session.
Let's verify by opening a new session and printing the variable's value.
But, we can make the definitions persistent as shown in the next section.
How to Make Environment Variables Persistent in Linux
To make the
JAVE_HOME variable persistent, edit the file
.bashrc and define its value in it.
.bashrc is a script file that's executed whenever a user logs in. It is hidden and located in the user's home directory by default.
I have edited my
.bashrc file as follows:
For the changes to take effect, update the
.bashrc file using the
Let's verify by opening a new session.
How to Create a Persistent Global Variable in Linux
Sometimes you might need to define a global environment variable that is accessible by all users.
For that, we need to first declare a variable and make changes in relevant files where environment variables are read from.
Let's go step by step.
- I am logged in as the user
Zaira. I am creating a global variable
zaira@Zaira:~$ export GLOBAL_VARIABLE="This is a global variable"
2. Edit the following files:
/etc/environment– This file is used to set up system-wide environment variables.
For the changes to take effect, use the command
/etc/profile– Variables set in this file are read whenever a bash shell is logged in. Edit this file and use the
Time to test!
Now, I'll switch the user to the root user and verify if I can access the variable
It worked! I have been able to access the global variable defined by the user
Zaira through the
root user as well. The same would apply to other users too. So now you also know how to define global environment variables.
In this tutorial, you learned how to create and define environment variables in Linux. You also learned how to make them persistent so that you can use them across multiple sessions.
What’s your favorite thing you learned here? Let me know on Twitter!
You can read my other posts here.
Image by catalyststuff on Freepik.