Shell Scripting - Local Variables
Last Updated :
02 Aug, 2022
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:-
Similar Reads
Shell Scripting - Shell Variables A shell variable is a character string in a shell that stores some value. It could be an integer, filename, string, or some shell command itself. Basically, it is a pointer to the actual data stored in memory. We have a few rules that have to be followed while writing variables in the script (which
6 min read
Shell Scripting - Rules for Naming Variable Name Variables are quite important in any script or program, so we need to understand what is the convention to name these variables. There are certain rules and standards to keep in mind while giving names to the variables in Shell scripting. In this article, we will discuss and list down all the rules
4 min read
Shell Scripting - Different types of Variables 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
4 min read
Shell Scripting - Default Shell Variable Value A shell gives us an interface to the Unix system. While using an operating system, we indirectly interact with the shell. On Linux distribution systems, each time we use a terminal, we interact with the shell. The job of the shell is to interpret or analyze the Unix commands given by users. A shell
3 min read
Shell Scripting - Variable Substitution A shell is an interface that helps users to connect with the system. Using a shell is equivalent to indirectly communicating with the operating system. In Linux distributed systems, each time we use a terminal, we connect with a shell. The job of a shell is to analyze Unix commands or instructions g
3 min read