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. 

Similar Reads

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

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

Overwriting of variable

See below example:...