Open In App

Shell Scripting – Different types of Variables

Last Updated : 28 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The shell is a command-line interpreter for Linux and Unix systems. It provides an interface between the user and the kernel and executes commands.  A sequence of commands can be written in a file for execution in the shell. It is called shell scripting. It helps to automate tasks in Linux.

Scripting language also has concepts of different types of variables like procedural or object-oriented languages. 

In shell scripting there are three main types of variables are present. They are –

  • Local Variables 
  • Global Variables or Environment Variables
  • Shell Variables or System Variables

We will discuss them one by one in this article –

Local Variable

A local variable is a special type of variable which has its scope only within a specific function or block of code. Local variables can override the same variable name in the larger scope. Let’s understand this concept using an example –

Shell script:

#!/bin/sh

getName(){
NAME=SATYAJIT #local variable
echo "$NAME (from function)" #valid if called using function
}

echo "$NAME - (outside function)" #invalid here 
getName 

Output:

 - (outside function)
SATYAJIT (from function)

In this example, NAME is a local variable and its scope is limited within the getName() function. So, when we try to call it from outside the function, there is nothing in output. Next, when we call the function then it’s displayed on the output screen.  Below is the terminal shell pictorial depiction after executing the following shell script :-

 

Global Variables

A global variable is a variable with global scope. It is accessible throughout the program. Global variables are declared outside any block of code or function.  Let’s understand this concept using an example –

Shell script:

#!/bin/sh

NAME=SATYAJIT #global variable

getName(){
echo "$NAME (from function)" 
}

echo "$NAME - (outside function)" 
getName 

Output:

SATYAJIT - (outside function)
SATYAJIT (from function)

Now, we have updated the earlier example code and declared the variable NAME outside any function. It makes the NAME variable a global variable that has its scope all over the program. Thus, it can be accessed from inside or outside a function. Below is the terminal shell pictorial depiction after executing the following shell script:-

 

Though, if we execute another shell script from a shell script, then local and global variables will be ignored by the new shell. Thus, if we want to make variables truly global, then we have to use the export command. Let’s understand this concept using an example –

Shell script:

gfg_1.sh

#!/bin/sh
NUM=9
echo "gfg_1 : The value is $NUM"
export NUM
./gfg_2.sh

gfg_2.sh

#!/bin/sh
echo "gfg_2 : The value is $NUM"

Output

gfg_1 : The value is 9
gfg_2 : The value is 9

Here, the NUM variable is accessible even from the other shell script (gfg_2.sh). This is because we have used the export command in gfg_1.sh . If we don’t use the export command, then the NUM variable value will not be visible from gfg_2.sh. Below is the terminal shell pictorial depiction after executing the following shell script –

 

Shell Variables

These are special types of variables. They are created and maintained by Linux Shell itself. These variables are required by the shell to function properly  They are defined in Capital letters and to see all of them, we can use set / env / printenv command. Below is the terminal shell pictorial depiction after executing the following command:-

 

Some useful shell variables are –

Variable Name Description Usage
BASH_VERSION  Holds the version of this instance of bash. echo $BASH_VERSION
HOME Provides a home directory of the current user.  echo $HOME
HOSTNAME Provides computer name echo $HOSTNAME
USERNAME Provides username echo $USERNAME

Below is the terminal shell pictorial depiction after executing the following commands-

 


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads