Environment Variables in Linux/Unix
What is an environment variable?
Environment variables or ENVs basically define the behavior of the environment. They can affect the processes ongoing or the programs that are executed in the environment.
Scope of an environment variable
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 which exists in the terminal. That means it can be used in all kind 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 variable 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 variable 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
$USER: Gives current user's name. $PATH: Gives search path for commands. $PWD: Gives the path of present working directory. $HOME: Gives path of home directory. $HOSTNAME: Gives name of the host. $LANG: Gives the default system language. $EDITOR: Gives default file editor. $UID: Gives user ID of current user. $SHELL: Gives location of current user's shell program.
EXAMPLE:



