Linux

How to set environment variable in Linux

System software and applications use a set of environment variables in order to fast access certain information. These variables serve as a storage and when software invoke them, they simply pass the stored information. Linux has a complete range of most common environment variables. The purpose of these common variables is to change the stored information for accommodating changed conditions. For example, below are the most commonly used environment variables in Linux:

Variable NamePurpose of the Variable
DisplayName of the graphical environment
EditorPreferred text editor
ShellName of the shell program
HomeStored path of the home directory
LangPreferred system language
UserStores your username
BrowserPath to the default browser
Bash_versiondisplays the version of the bash shell used
PathDisplays a colon-separated list of the paths of the user executable programs
Most common environment variables found in Linux

Set Environment Variable in Linux

After knowing the most common environment variables in Linux, now you need to know how to alter those variables. Sometimes a certain software fails to run because it can’t find the required variable, or it’s unable to process the information stored within. Below are the most common commands related to environment variable in Linux:

CommandPurpose of Command
envLists all the environment variables in terminal
exportExports the value of a newly assigned environment variable to the system
setChanges the flags of environment variables as defined by the user for example set -a is used to mark variables that are modified or created for export.
unsetRemoves the associated flags of an environment variable
Common commands used for environment variables in Linux

For example, let’s learn how to modify the HOME environment variable. Use the following command in the terminal:

HOME="/home/username"

After you’ve set the value of environment variable HOME now you need to export it for other programs to use it, following command works:

export HOME

This will export your new data to the other programs and subshells. Similarly, $ANDROID_HOME is another variable which is used while running Android SDK used for developing android applications. You can set it using the following command:

export ANDROID_HOME=~/Android/Sdk

It gives the home directory/Android/Sdk path to the executing program while building Android applications. Note that $ dollar sign is not followed when you set an environment variable’s value.

Conclusion:

If you want to change an environment variable in Linux then it’s an easy task. You can simply launch a terminal, set the new value and later export it to make it useable for other programs. If you want to do the same on Mac OS Catalina then read: Learn how to set a permanent environment variable in Catalina. Similarly, you can also add environment variables on Windows 10.

It’s important to know that if your environment variable in linux resets after restart then you need to add a persistent one. For that purpose, you need to find your shell file in your home directory, in most cases its path is ~/.bash_profile or simply ~/.profile. Adding your variables inside the file will make sure that they remain persistent on every restart.