Environment variables are a type of variables that are defined in the shell and are required during program execution. They are often used in shell programs to perform various operations.
The scope of any variable refers to the area in which it can be accessed or defined. In Linux, the scope of an environment variable can either be global /system-wide or local.
In this context, we shall look into how to set and unset environment variables in Linux.
Common examples of environment variables in Linux includes:
- PWD – displays the path of the present working directory.
- HOME – gives the path of the home directory.
- HOSTNAME – prints name of the host.
- EDITOR – shows the default file editor.
How to Set environment variables using the export command ?
A simple way to set environment variables is using the export command. We have to give the variable a name that will be used to access it in the shell and a value to hold data with the below command:
$ export NAME = VALUE
For example, set you variable name:
$ export VAR ="value"
When using the export command, the environment variable will be set only for the present shell session. As a result, the environment variable will no longer be accessible if you open a new terminal or restart your system.
To output the value of your environment variable on the shell, you can use either of the commands:
$ printenv variable
$ echo $variable
Note: When using the echo command, the variable name should be preceded by a dollar sign.
How to Set user-wide environment variables on Linux ?
These are the variables that have been defined for a specific user and are executed whenever that user logs in via a local or remote login session. They are set in and loaded from the following configuration files in the user's home directory: bashrc, .bash profile, .bash login, and .profile.
i. Using the .bashrc file
The .bashrc file is a script that is loaded whenever a user opens a new terminal session. Environment variables in this file are executed whenever a new session is started.
For example, add a variable NEW_VAR to your .bashrc file with the below command:
$ sudo vi .bashrc
$ export NEW_VAR ="Linux"
Then, Save your file and reload the .bashrc file with the following source command for changes to be applied:
$ source ~/.bashrc
Print out the new variable:
$ echo $NEW_VAR
The variable is now persistent when you open new sessions or restart your system.
ii. Using .bash_profile
To add environment variables that will be available for remote login sessions., modify the .bash_profile file:
$ sudo vi .bash_profile
$ export $VAR1 ="Linux"
Next, reload the .bashrc file with the following source command for changes to take effect:
$ source ~/.bash_profile
Print out the new variable:
$ echo $VAR1
How to Set system-wide environment variables in Linux ?
These are system-wide environment variables, meaning they are available to all users on the system. These variables can be found in the following directories and files that include system-wide configuration files: /etc/environment, /etc/profile, /etc/profile.d/, /etc/bash.bashrc, /etc/profile.d/, /etc/profile.d/, /etc/profile.d/, /etc/profile.d/, /etc/profile.
Using /etc/bash.bashrc file
Add a system-wide environment variable to the /etc/bash.bashrc file. It will be available to the users when any of them starts a new terminal but cannot be accessed remotely:
$ export VAR1='Linux'
Now, reload the /etc/bash.bashrc file for changes to take effect:
$ source /etc/bash.bashrc
Here, the variable can be accessed by both the normal user and root.
Using /etc/profile file
To add an environment variable that will be available when any of the users on your system is accessing it remotely, modify the ‘/etc/profile’ file. After adding the variable, reload the file:
$ sudo vi /etc/profile
$ export VAR='Linux'
$ source /etc/profile
$ echo $VAR
Here, the variable is available for a new user who has logged in remotely.
Using /etc/environment file
To add an environment variable that will be available to all users on both local and remote login sessions, add the variable in the /etc/environment file:
$ sudo vi /etc/environment
$ export MY_VAR='Linux'
$ source /etc/environment
$ echo $MY_VAR
How to Unset Environment Variables on Linux ?
Now that you have learnt how to set environment variables on your system, you may need to remove them if you don't need them anymore.
To unset environment variables in Linux use the unset command or remove variable entries from your system configuration files.
Use the following syntax to unset an environment variable in Linux:
$ unset <variable>
For example, unset any of the above variables:
$ unset NEW_VAR
To verify this, print out the variable:
$ echo $NEW_VAR
You will notice that nothing is displayed in the terminal.
Note: Environment variables defined in the system-wide or user-wide configuration files can be erased solely by removing them from these files. After that, reload the file with the source command for changes to take effect:
$ source <file-name>
[Need to configure Environment Variables on your Linux system ? We can help you. ]
This article covers how to set and unset both local and persistent environment variables in Linux. In fact, every time you start a shell session in Linux, the system goes through configuration files and sets up the environment accordingly. Environment variables play a significant role in this process.
Environment variables are a set of key value pairs stored on your Linux and used by processes in order to be able to perform specific operations. with the export command but also by modifying some system files to make them persistent.
How to Set Environment Variables on Linux using export ?
The easiest way to set environment variables is to use the export command:
$ export VAR="value"
How to Unset Environment Variables on Linux Using unset command ?
To unset an environment variable, use the unset command with the following syntax:
$ unset <variable>
Common Set of Environment Variables on Linux:
- USER : the current username of the user using the system;
- EDITOR : the program run to perform file edits on your host;
- HOME : the home directory of the current user;
- PATH : a colon separated list of directories where the system looks for commands;
- PS1 : the primary prompt string (to define the display of the shell prompt);
- PWD : the current working directory;
- _ : the most recent command executed on the system (by the user)
- MAIL : the path to the current user’s mailbox;
- SHELL : the shell used in order to interpret commands on the system, it can be many different ones (like bash, sh, zsh or others);
- LANG : the language encoding used on the system;
- DESKTOP_SESSION : the current desktop used on your host (GNOME, KDE)
- HISTFILESIZE : number of lines of command history stored in the history file;
- HISTSIZE : number of lines of history allowed in memory;
- UID : the current UID for the user.