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