Open In App

Batch Script – Local VS Global Variables

Improve
Improve
Like Article
Like
Save
Share
Report

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. 

Local VS Global variable:

Local variable Global variable
The ‘local’ keyword is used to declare the local variable. No keyword is used.
It is declared inside the function It can be declared outside the function anywhere in the program.
It doesn’t prove the data sharing It provides data sharing
These are stored on the stack These variables are stored in the fixed memory location by the compiler.
Parameters passing is necessary Parameters passing is not necessary
Garbage value is stored if not initialized. Zero is stored by default if not initialized.
If modified inside one function that modification will not get reflected in other function. If modified in one function that modification will be visible in whole program.

Advantages of using Local Variable

  • 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.

Disadvantages of using Local variable

  • 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.

Advantages of using Global Variable

  • 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.

Disadvantage of using Global Variable

  • 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.

Function

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.

Example 1: Creating local variable

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 = 

Example 2: Creating Global Variable

#!/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

Example 3: Multiple local and global variable in a program

#!/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!

Overwriting of variable

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!!

Last Updated : 04 Jan, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads