Environment Variables in Linux/Unix
Environment variables, often referred to as ENVs, are dynamic values that wield
significant influence over the behavior of programs and processes in the Linux
operating system. These variables serve as a means to convey essential
information to software and shape how they interact with the environment. Every
Linux process is associated with a set of environment variables, which guide its
behavior and interactions with other processes.
Table of Content
• Accessing Environment Variables
• Scope of an environment variable
• Global Environment Variables
• Local Environment Variables
• Displaying Environment Variables
• How to Set Environment Variables in Linux?
• To Set User-Wide Environment Variables in Linux
• To Set System-Wide Environment Variables in Linux
• How to unset environment variables?
• Some commonly used ENVs in Linux
Accessing Environment Variables
In Linux, the primary conduit for interacting with environment variables is the
shell. The shell acts as a command-line interpreter, executing instructions entered
by the user. The most prevalent shell in the Linux world is the Bash shell (Bourne
Again SHell), which comes as the default in many Linux distributions.
Scope of an environment variable
Understanding the scope of an environment variable is crucial. It dictates where
the variable can be accessed or defined, making a clear distinction between global
and local scopes.
Global Environment Variables
A global environment variable, when defined in a terminal, enjoys accessibility
from anywhere within that terminal’s environment. This means it can be
employed by scripts, programs, or processes running within the scope of that
terminal.
Local Environment Variables
On the other hand, local environment variables are confined to the specific
terminal in which they are defined. They are isolated from external programs or
processes, making their reach exclusive to the terminal that birthed them.
Accessing Environment Variables
Accessing environment variables is straightforward and consistent, regardless of
their scope. To retrieve the value of an environment variable, the following syntax
is employed:
Syntax
VARIABLE_NAME
This syntax is uniform for both local and global environment variables. Displaying
Environment Variables
To display the value of an environment variable, the echo command is employed,
as demonstrated below:
Syntax:
echo $VARIABLE_NAME
To obtain a list of all global environment variables, Linux offers several
commands:
1. 'printenv' Command in Linux
This command provides a comprehensive list of all global environment variables.
printenv //displays all the global ENVs
2. 'set' Command in Linux
Lists all environment variables, encompassing both global and local variables.
set //display all the ENVs(global as well as local)
3. 'env' Command in Linux
Presents a list of global environment variables.
env //display all the global ENVs
archana@archana-pc: -
File Edit View Search Terminal Help irchana@archana-pcSITE=’GEEKSFORGEEKS'
irchana@archana-pc:~$ printenv | grep SITE irchana@archana-pc:~$ env | grep SITE
irchana@archana-pc:~$ set | grep SITE >ITE=GEEKSFORGEEKS
irchana@archana-pc:~$ export SITE='GEEKSFORGEEKS' >rchana@archana-pc:~$ printenv | grep
SITE iITE=GEEKSFORGEEKS
irchana@archana-pc:~$ env | grep SITE iITE=GEEKSFORGEEKS
irchana@archana-pc:~$ set | grep SITE
iITE=GEEKSFORGEEKS
irchana@archana-pc:~$
How to Set Environment Variables in Linux?
Environment variables can be set using different approaches, depending on their
intended scope.
1. Setting Global Environment Variables
Global environment variables can be defined using either the export or set
command. Both methods involve specifying the variable’s name and value. For
example:
$ export NAME=Value or
$ set NAME=Value
Example:
archana@archana-pc: ~
File Edit View Search Terminal Help
archana@archana-pc:~$ export A=1
archana@archana-pc:~$ echo $A 1
±
archana@archana-pc:~$ |
2. Setting Local Environment Variables
Local environment variables can be directly defined without the need for export
commands. The syntax is as follows:
Syntax:
$ NAME=Value
Example:
archana@archana-pc: ~ 600
File Edit View Search Terminal Help
archana@archana-pc:~$ B=1
archana@archana-pc:~$ echo $B
1
archana@archana-pc:~$ |
To Set User-Wide Environment Variables in Linux
User-wide environment variables are configured within user-specific files like
'~/.bashrc', '~/.bash_profile', '~/.bash_login', or '~/.profile'. These variables are
personalized and persist across system restarts. The following steps guide the
process of setting user-wide environment variables:
Following steps can be followed to do so:
Step 1: Open the terminal.
Step 2: Edit the user-specific file (e.g., ~/.bashrc) using a text editor. Step
3: Insert the variable in the file with the export statement: export
NAME=Value
Step 4: Save and close the file. Step 5:
Activate the changes using: $ source
~/.bashrc
Example:
To Set System-Wide Environment Variables in Linux
System-wide environment variables are configured in files such as
'/etc/environment', '/etc/profile', '/etc/profile.d/', or
'/etc/bash.bashrc'. These variables are accessible to all users and remain
persistent across system restarts. Setting a system-wide environment variable
involves the following steps:
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: To apply the changes, log out and log in again
How to unset environment variables?
To unset an environment variable, the unset command is used, or the variable can
be assigned an empty value. For instance:
Syntax:
$ unset NAME or
$ NAME-'
Example:
archana(®archana-pc: ~
File Edit View Search Terminal Help
archana@archana-pc:~$ export A=1
archana@archana-pc:~$ echo $A 1
1
archana@archana-pc:~$ unset A
archana@archana-pc:~$ echo $A
archana@archana-pc:~$ |
Permanent environment variables can be unset by editing the corresponding
configuration files and removing the lines where they were initially defined. Some
commonly used ENVs in Linux
Environment Variables Description
$USER Gives search path for commands.
$PATH Gives search path for commands.
$HOME Gives path of home directory.
$PWD Gives the path of present working 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:
archana@archana-pc: ~ *©©
File Edit View Search Terminal Help archana@archana-pc:~$ echo $USER archana
archana@archana-pc:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/ games:/snap/bin
archana@archana-pc:~$ echo $PWD /home/archana
archana@archana-pc:~$ echo $HOME /home/archana
archana@archana-pc:~$ echo $HOSTNAME archana-pc
archana@archana-pc:~$ echo $LANG en_IN
archana@archana-pc:~$ echo $UID 1000
archana@archana-pc:~$ echo $SHELL /bin/bash
archana@archana-pc:~$_
Conclusion
So far, 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.
•k-kick-kick-kick