Environment Variables in Linux/Unix - GeeksforGeeks

30 Jul, 2021
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 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. 
SYNTAX:  
 $NAME 
NOTE: Both local and global environment variables are accessed in the same way.  
SYNTAX:  
$ echo $NAME 
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:  
To set a global ENV  
$ export NAME=Value
or
$ set NAME=Value
EXAMPLE:  
SYNTAX:  
$ NAME=Value
EXAMPLE:  
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:  
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. 
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.  
$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:  
 
rajeev0719singh
linux-command
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
TCP Server-Client implementation in C
ZIP command in Linux with examples
tar command in Linux with examples
curl command in Linux with Examples
UDP Server-Client implementation in C
Conditional Statements | Shell Script
Cat command in Linux with examples
Tail command in Linux with examples
touch command in Linux with Examples
Mutex lock for Linux Thread Synchronization