Environment Variables in Linux

March 26, 2021

Environmental variables are inherited by any subshells or child processes, for example, HOME, PATH. Every shell Terminal has a memory area called the environment. Shell keeps all details and settings in the environment. When we start a new Terminal or shell, this environment is created every time.

We can view the environment variables with the following command:

$ env

Or we can use this:

$ printenv

The output of the $ env command is as follows:

Output:

satish@backup:~$ env
XDG_SESSION_ID=169933
TERM=xterm
SHELL=/bin/bash
SSH_CLIENT=103.146.216.131 1415 22222
SSH_TTY=/dev/pts/0
USER=satish
MAIL=/var/mail/satish
PATH=/home/satish/bin:/home/satish/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
PWD=/home/satish
LANG=en_US.UTF-8
SHLVL=1
HOME=/home/satish
LOGNAME=satish

The list of environment variables will be quite extensive. I advise you to browse through the complete list. We can change the content of any of these environment variables.

Environmental variables are defined in a Terminal or shell. They will be available in any subshells or child shells created from the current shell Terminal. You will learn about these activities in the next few sections. You have already learned that every command in a shell creates a new subshell from the current shell.

The following is a brief summary of a few environmental variables:

Variable

Description

HOME

The user’s home directory

PATH

The search path for commands

PWD

Current working directory

IFS

The internal field separator; that is, the character that separates individual arguments from one another

PS1

The primary shell prompt

PS2

The secondary shell prompt

PS3

The tertiary shell prompt (see select)

?

The exit status or (return value) of the most recent child process

$

The process ID of the current shell itself

#

The number of arguments passed to the shell

0-9

Argument 0 (usually the command itself), argument 1, and so on, as passed to the shell

*

All arguments (with the exception of argument 0) as separate words or arguments

@

All arguments (with the exception of argument 0) as separate words or arguments

Whenever any user logs in, the /etc/profile shell script is executed.

For every user, the .bash_profile Shell script is stored in the home folder. The complete path or location is /home/user_name/.profile.

Whenever a new Terminal is created, every new Terminal will execute script .bashrc, which is located in the home folder of every user.

The local variable and its scope

In the current shell, we can create and store user-defined variables. These may contain characters, digits, and _. A variable should not start with a digit. Normally, for environment variables, uppercase characters are used.

If we create a new variable, it will not be available in the subshells. The newly created variable will be available only in the current shell. If we run a Shell script, then the local variables will not be available in the commands called by the Shell script. Shell has one special variable, $$. This variable contains the process ID of the current shell.

Let’s try a few commands:

This is the process ID of the current shell:

$ echo $$1234

We declare the variable name and initialize it:

$ name="Ganesh Naik"$ echo $nameGanesh Naik

This command will create a new subshell:

$ bash

This is the process ID of the newly created subshell:

$ echo $$1678

From the following, nothing will be displayed, as the local variables from the parent shell are not inherited in the newly created child shell or subshell:

$ echo $name

We will exit the subshell and return to the original shell Terminal:

$ exit

This is the process ID of the current shell or parent shell:

$ echo $$1234

This displays the presence of the variable in the original shell or parent shell:

$ echo $nameGanesh Naik

Variables created in the current shell will not be available in a subshell or child shell. If we need to use a variable in a child shell as well, then we need to export it using the export command.

Exporting variables

We can use the export command to make variables available in the child process or subshell. But if we declare new variables in the child process and export it in the child process, the variable will not be available in parent process. The parent process can export variables to a child, but the child process cannot export variables to the parent process.

Whenever we create a Shell script and execute it, a new shell process is created and the Shell script runs in that process. Any exported variable values are available to the new shell or to any sub-process.

We can export any variable as follows:

$ export NAME

Or we can use this:

$ declare -x NAME

Let’s try to understand the concept of exporting the variable, using the following example:

$ PERSON="Ganesh Naik"
$ export PERSON
$ echo $PERSON
Ganesh Naik
$ echo $$515

The process ID of the current shell or parent shell is 515.

This will start a subshell:

$ bash

This is the process ID of new or sub-shell:

$ echo $$526

Let us check the presence of variables:

$ echo $PERSON
Ganesh Naik
$ PERSON="Author"
$ echo $PERSONAuthor
$ exit

This will terminate the subshell, and it will be placed in the parent shell:

$ echo $$
515

This displays the presence of the variable in the original shell or parent shell:

$ echo $PERSON
Ganesh Naik

Let’s write a shell script to use the concept we have learned:

# Ubuntu Timezone files location : /usr/share/zoneinfo/ 
# redhat "/etc/localtime"  instead of "/etc/timezone" 
# In Redhat 
# ln -sf /usr/share/zoneinfo/America/Los_Angeles /etc/localtime 
 
export TZ=America/Los_Angeles 
echo "Your Timezone is = $TZ" 
date 
export TZ=Asia/Tokyo 
echo "Your Timezone is = $TZ" 
date 
 
unset TZ 
 
echo "Your Timezone is = $(cat /etc/timezone)" 
# For Redhat or Fedora /etc/localtime 
date

Output:

Your Timezone is = America/Los_Angeles
Thu Mar 18 21:06:57 PST 2021
Your Timezone is = Asia/Tokyo
Fri Mar 19 14:06:57 JST 2021
cat: /etc/timezone: No such file or diectory
Your Timezone is = 
Fri Mar 19 10:36:59 IST 2021

The date command checks the TZ environmental variable. We initialized the TZ for Los_Angeles, then to Tokyo, and, finally, we removed it. We can see the difference in the date command output.

Let’s write another Shell script to study the parent and child process, and the export of variables.

Create the export1.sh shell script:

export1.sh

#!/bin/bash 
foo="The first variable foo" 
export bar="The second variable bar" 
./export2.sh 
 
Create another shell script export2.sh 
#!/bin/bash 
echo "$foo" 
echo "$bar" 
satish@backup:~$
satish@backup:~$./export1.sh

The second variale bar
satish@backup:~$

The shell script export1.sh runs as a parent process and export2.sh is started as a child process of export1.sh. We can clearly see that variable bar, which was exported, is available in the child process, but the variable foo, which was not exported, is not available in the child process.

Related Articles

Calculating and reducing the runtime of a script

In this article, we are going to learn how to calculate and reduce the script’s runtime. A simple time command will help in calculating the execution time.PrerequisitesBesides having a terminal open, make sure you have the necessary scripts present in your...

read more

Lorem ipsum dolor sit amet consectetur

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

2 × four =