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