Batch Script – Local VS Global Variables

04 Jan, 2022
In this article, we will see the differences between local and global variables in bash scripting.
Variable: The name given to a memory location that is used to store values in a program is called variables. It stores information that can be called and manipulated wherever needed in the program.
Scope: The part or extent of the program where the variable is accessible or said to be alive.
Based on the scope of the variable, it is of two types:
Local variable: those variables whose scope is within the function where it is declared( can be accessed within the declared block or function in the program)
Global variable: these variables can be accessed globally in the entire program. 
It ensures that the values of variables will remain intact while the task is running.
In multitasking environments where each task can change the value of a variable. It will not produce undesirable results as each task can create its own instance of the local variable.
The same variable name can be used in different tasks.
It releases memory as soon as it gets executed.
It makes debugging a complex process.
Due to the restriction of data sharing between modules, common data need to pass repeatedly.
They have a very limited scope.
It can be accessed from all the functions or modules of the program.
One needs to declare the global variable only once outside the modules.
It is used for storing constant values to maintain consistency.
It is useful when many functions have the access to the same data.
All the global variable that is declared inside the program remains in the memory till execution gets completed. This may cause Out of Memory issues.
Modification of data is easy, it can be easily modified by any task or function. This can produce undesirable results in multi-tasking environments.
Discontinuation of global variable due to refactoring will cause the problem of changing all the associated modules.
Let’s see some example to understand the global and local variables better.
The function is the set of commands that can be called multiple times. It is used to avoid writing the same block of code again and again.
Syntax:
function_name () {
 commands
}
OR
function function_name {
  commands
}
Both the format of writing functions is correct in bash scripting.
The local variable is declared by using the keyword before the variable name
#!/bin/bash
function first () {
  local x="HelloNew"
 echo "Inside first function x=$x"
 
}

first
echo "Outside first function x = $x"
Output:
Inside first function x=HelloNew
Outside first function x = 
#!/bin/bash
function first () {
 x="Hello Geek"
 echo "Inside first function x= $x"
 
}

first
echo "Outside first function x = $x"
Output:
Inside first function x=Hello Geek
Outside first function x = Hello Geek
#!/bin/bash
function first () {
 local x="Hello Geek"
 local y= "Enjoy"
 echo "Inside first function x= $x"
 echo "Inside first function y= $y"
 
}
x="Hello Romy"
y="good to see you!"
first
echo "Outside first function x = $x"
echo "Outside first function y= $y"
Output:
Inside first function x= Hello Geek
Inside first function y=  
Outside first function x = Hello Romy
Outside first function y= good to see you!
See below example:
#!/bin/bash
x="Hellooooo!!"
echo " Outside first function x =$x"
function first () {
 x="Hello Geek"

}

first
echo "After overwriting, x = $x"
Output:
Outside first function x =Hellooooo!!
After overwriting, x = Hello Geek
From the example, we can see that the value of x is overwritten. To prevent the overwriting of variables we can make our variable readonly by writing ‘declare -r’ before variable declaration.
Example:
#!/bin/bash
x="Hellooooo!!"
echo " Outside first function x =$x"
function first () {
 declare -r x="Hello Geek"
}

first
echo "After overwriting, x = $x"
Output:
Outside first function x =Hellooooo!!
After overwriting, x = Hellooooo!!
Batch-script
Picked
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Docker - COPY Instruction
scp command in Linux with Examples
chown command in Linux with Examples
SED command in Linux | Set 2
nohup Command in Linux with Examples
mv command in Linux with examples
chmod command in Linux with examples
Array Basics in Shell Scripting | Set 1
Introduction to Linux Operating System
Basic Operators in Shell Scripting