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.
Batch Script – Local VS Global Variables
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.