Open In App

Shell Scripting – Local Variables

Last Updated : 02 Aug, 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.

There are three main types of variables are present in shell scripting. They are – Shell Variable, Global Variable, and Local Variable.

Shell variables are special types of variables. They are created and maintained by Linux Shell itself. These variables are required by the shell to function properly.

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.

In this article, we are going to discuss Local Variables.

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. 

We will understand all the concepts related to Local variables using different examples.

Example 1:  Limited Scope

This example depicts that a local variable written inside a function has its scope limited only within the function.

Shell Script:

#!/bin/sh

getNUM(){
    NUM=100 #local variable
    echo "$NUM - inside function" 
}

echo "$NUM - outside function" 

getNUM 

Output:

 - outside function
100 - inside function

In this example, NUM is a local variable because it is within the getNUM() function. When we are accessing the value of it from the function itself, then we are getting it, but when we are trying to access the same from outside the function, it is not visible.

Below is the terminal shell pictorial representation after executing the following script:- 

 

Example 2: Overrides Global Variable

This example depicts that a local variable can override the global variable in its scope.

Shell Script:

#!/bin/sh
NUM=200 #global variable
getNUM(){
    NUM=100 #local variable
    echo "$NUM - inside function" 
}

echo "$NUM - outside function" 

getNUM 

Output:

200 - outside function
100 - inside function

Here, there is a global variable and a local variable within the getNUM() function having the same name. The local variable within its scope i.e. the getNUM() function, override it but outside its scope, the global variable is accessed.

Below is the terminal shell pictorial representation after executing the following script:- 

 

Example 3: Recursion and Local Variable

Local variables make recursion possible. Each function call in recursion creates its own set of local variables. 

This example depicts a shell program to print n to 0 using recursion. 

Shell Script: 

#!/bin/sh

printNUM(){
    if [ $1 -lt 0 ];then
    exit
    fi
    echo $1
    printNUM $(($1-1))
}
printNUM 5

Output:

5
4
3
2
1
0

Here, positional argument $1 is acting as a local variable, making the recursion possible.

Below is the terminal shell pictorial representation after executing the following script:-

 


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads