The environment variable is a pair of keys and values stored within the system and can be accessed by an application from the shell or sub-shell.
You can set environmental variables for your programs, such as user preferences, long commands into the more minor abbreviations, system locale, the path of the executable file, development environment variables, etc.
Table of Contents
Scope of an Environment Variable in Linux
The scope of the variable is defined into two categories:
Global Environment Variable
A global variable is persistent and accessible from anywhere, from shell to sub-shell. This means any application or shell script can easily access this persistent variable for their work.
Local Environment Variable
A locally scoped variable is only accessible from the running shell. Once the shell exits, all the local variables will be flushed.
Common Environment Variables in Linux
A list of the standard environment variables you might encounter while working on Linux.
Environment Variables | Definition |
PATH | This variable stores the list of paths to look for while executing the command. All directories are separated with a (:) colon |
USER | Holds the username |
HOME | Holds the default path of the currently logged user home directory |
EDITOR | Holds the default text editor to use when required |
UID | Hold the user’s unique ID |
TERM | Define the terminal type |
SHELL | It contains the default shell that the user is using |
PWD | Display the current working directory |
LOGNAME | Carries the name of the user |
LANG | Holds the default language being used in the system |
How to Check Existing Environment Variables in Linux?
There are three different ways to list the current environment variables in your system.
env
can help to set the local environment variable. If it already exists, the existing one will be replaced for the current session. If the command is executed without an argument, it will list the present environment variables in your system.printenv
will list the present environment variables or specified variable values.echo $
with the variable name will display its value.
The output of the env
command without an argument and the printenv
command share common output and display the global environment variables on your system, as shown below.
$ env
OR
$ printenv
Below is the output of the above command.
If you want to check the value of the variable name, you can use printenv
or echo $
by specifying the name of the variable, as shown below.
$ printenv LOGNAME
OR
$ echo $LOGNAME
Below is the output of the above command.
Note: While using echo, specify “$” at the beginning of the variable name but not for the printenv command.
Setting Environment Variables
As we discussed earlier, we have multiple scopes (global and local) for an environment variable in Linux. First, we will start with the local variables.
The easiest way to set a local variable is to directly specify the variable name with its value, as shown below.
$ favsite=trendoceans
If you try to check the value using the printenv
or echo $
command, the value of the variable name “favsite” will not be displayed.
However, you can use the set command by grepping to the variable name to display the output, but setting the environment variable using this method will not work inside the sub-shell, as shown below.
$ set | grep favsite
Below is the output of the above command.
Above you can note that creating a variable with this method will only display the value of the variable name only in the parent shell or the shell where the variable was declared, but creating a sub-shell, in this example, was created using the bash
command will not output the variable value.
This method seems good with only one problem the variable is not being shared with the sub-shell. So, what should we do?
In this case, we can add one extra keyword export
at the beginning of the variable declaration to make it accessible within the shell and sub-shell, as shown below.
$ export favsite=trendoceans
To output the value of this variable, you can use printenv
it by specifying the variable name or echo $[VARIABLE NAME]
to output the variable value, as shown below.
$ printenv favsite
OR
$ echo $favsite
Below is the demonstration of the above command showing how using this method help us to access the variable value in shell and sub-shell.
In simple words, you can set a variable by directly specifying the key name and value (favsite=trendoceans), but by this method, the variable will only work in the parent shell, not in the sub-shell. Another way is by using the export
command to set variables which can be shared in the sub-shell.
Persistent Environment Variables
Currently, both methods do not persist the environment variable, making them inaccessible after the shell session is closed or restarted.
To make it persistent, we can add those variables inside the shell configuration file, which loads the environment variable every time the shell is loaded.
This configuration file location varies depending upon the shell you are using. To make it easy, execute the below command to find your current shell.
$ printenv SHELL
Below is the output of the above command.
For me, it’s a bash shell, and most distributions also provide bash as a default shell. So, for the bash shell, the configuration file is located at the ~/.bashrc
and you can add your environment variable at the end of the file content by using your favourite text editor or executing the below command.
$ echo 'export favsite=trendoceans' >> ~/.bashrc #Add environment variable inside the ~/.bashrc making them permenent environment variable.
$ tail -1 ~/.bashrc #Output the last line of the ~/.bashrc file.
$ source ~/.bashrc #Reload the bash configuration for the current shell session.
Below is the output of the above command.
Note: I am using the export command to set the permanent environment variable, making them accessible within the shell and sub-shell.
If you are new to Linux, then edit the bash configuration file and manually add the “export favsite=trendoceans
” at the end of the file, but don’t forget to source ~/.bashrc
file.
The source ~/.bashrc
command helps us to directly reflect changes made in the configuration file. Otherwise, you have to restart the shell session to see the change made in the configuration file.
Once the shell configuration file is reloaded, you can use printenv
with the environment variable name to get the value of the environment variable.
Removing the Local Environment Variables
If you were using a local environment variable using the export command, it would automatically be removed from the shell after a session restart, or you can manually remove it from your current shell session using the unset command with the variable name, as shown below.
$ unset favsite
Below is the output of the above command.
Removing Persistent Environment Variables
Setting a persistent environment variable means adding the environment into the shell configuration file. For bash, if you set the environment variable inside the ~/.bashrc
configuration file, then remove it manually by opening the file using your favourite text editor, as shown below.
$ nano ~/.bashrc #Remove the line with environment variable.
$ source ~/.bashrc #Reloaded the shell configuration file.
Below is the output of the above command.
From above, if you were thinking about why I used the unset command after reloading the shell configuration file, the reason was that the environment variable still stays in the local scope. Either unset or restart the shell session to remove it altogether.
Below are some of the known examples of using the environment variable.
To set the Java Environment Variable, execute the command as follows:
$ export PATH=$PATH:/home/jdk1.8/bin/
To make the command executable from anywhere, run the command as follows:
$ export wireshark=/path/to/wireshark
If you have more compelling examples, then do share them with us in the comment section.
Innovative tech mind with 12 years of experience working as a computer programmer, web developer, and security researcher. Capable of working with a variety of technology and software solutions, and managing databases.
Consider putting all of your variables in ~/.profile if you need them in a login shell and in ~/.bash_profile if you need them in an interactive bash shell. The ~/.bashrc should source both ~/.bashrc and ~/.profile.
I just like the helpful information you provide in your articles