Environment variables or ENVs basically define the behavior of the environment. They can affect the ongoing processes or programs executed in the environment. Every Linux process has an associated set of environment variables that influence its behavior and interactions with other processes.
Accessing Environment Variables
In Linux, environment variables are typically accessed through the shell. The shell is a command-line interface that interprets and executes commands entered by the user. It provides a way to set, modify, and retrieve environment variables. The most common shell in Linux is the Bash shell (Bourne Again SHell), which is the default shell for many distributions.
Scope of an environment variable
The scope of any variable is the region from which it can be accessed or over which it is defined. An environment variable in Linux can have global or local scope.
Global
A globally scoped ENV that is defined in a terminal can be accessed from anywhere in that particular environment that exists in the terminal. That means it can be used in all kinds of scripts, programs, or processes running in the environment bound by that terminal.
Local
A locally scoped ENV that is defined in a terminal cannot be accessed by any program or process running in the terminal. It can only be accessed by the terminal (in which it was defined) itself.
How to access ENVs?
Syntax:
$NAME
NOTE: Both local and global environment variables are accessed in the same way.
How to display ENVs?
To display any ENV
Syntax:
$ echo $NAME
To display all the Linux ENVs
Syntax:
$ printenv //displays all the global ENVs
or
$ set //display all the ENVs(global as well as local)
or
$ env //display all the global ENVs
Example:


How to set environment variables?
To set a global ENV
$ export NAME=Value
or
$ set NAME=Value
Example:

To set a local ENV
Syntax:
$ NAME=Value
Example:

To set user wide ENVs
These variables are set and configured in ~/.bashrc, ~/.bash_profile, ~/.bash_login, ~/.profile
files according to the requirement. These variables can be accessed by a particular user and persist through power offs.
Following steps can be followed to do so:
Step 1: Open the terminal.
Step 2:
$ sudo vi ~/.bashrc
Step 3: Enter password.
Step 4: Add variable in the file opened.
export NAME=Value
Step 5: Save and close the file.
Step 6:
$ source ~/.bashrc
Example:

To set system wide ENVs
These variables are set and configured in /etc/environment, /etc/profile, /etc/profile.d/, /etc/bash.bashrc files according to the requirement. These variables can be accessed by any user and persist through power offs.
Following steps can be followed to do so:
Step 1: Open the terminal.
Step 2:
$ sudo -H vi /etc/environment
Step 3: Enter password.
Step 4: Add variable in the file opened.
NAME=Value
Step 5: Save and close the file.
Step 6: Logout and Login again.
How to unset environment variables?
Syntax:
$ unset NAME
or
$ NAME=''
Example:

Note: To unset permanent ENVs, you need to re-edit the files and remove the lines that were added while defining them.
Some commonly used ENVs in Linux
|
Gives search path for commands. |
Gives search path for commands. |
Gives path of home directory. |
Gives the path of present working directory. |
Gives name of the host. |
Gives the default system language. |
Gives default file editor. |
Gives user ID of current user. |
$SHELL | Gives location of current user’s shell program. |
Example:

Conclusion
In this article we have discussed Environment Variable which are essential components of the Linux operating system., allowing processes and applications to access important configuration information. Overall, we can say it is important for a Linux User to understand the concepts of Environment Variable.