Updated April 13, 2023
Introduction to Bash Local Variables
Before we even move a specific type of the variable and that is the local variable, let us first get a full context of variables as a terminology. Anyone who already has worked on other programming languages might be quite comfortable with the concept of a variable, but for someone who is a newbie to this, may find a bit hard to interpret what variable is. A variable nothing but a temporary storage area that enables data to be stored. This concept is very similar to the one in mathematics, where the variable is something to do with varying values. Similarly, in the case of programming language, we consider it as a temporary storage area that allows the variable to mutate itself in accordance with the requirement of the coding, and with operations on the variable might lead to changing of the values of the variables.
Now that we know the concept of variables it becomes imperative to know what different kinds of variables exist in the world of programming. In the context of different kinds of variable types, one of them is local and the other is global. The very first and very important context is that bash variables need not be declared. In having this flexibility, it doesn’t even mean that one might be able to access a variable that is not initialized. Hence, by just initializing, one can build a variable and be able to access the same. Now in this condition, if the spread of the access of the variable we have created spans across the entire code, it is termed to be a global variable, which essentially means that the use and access of the variable is not restricted to a function or a small portion of the code. In the contrary, when we talk about local variables, we mean that the use of the variable is just restricted to be used within a small portion of a larger code which for example might be a function. The variable initialized within a function is accessible only to that function and can never be accessed outside and hence the scope is local.
Why do we want to Use Bash Local Variables?
Now we would wonder why one would want to use local variables. In some big codes we would just want some variables to be used as a counter or something similar and begin and end the scope only for the required time and in doing so, once the use is over the memory is freed up for other use. In case you want to have a look at the same in action, just try to build a code that will recursively call itself and keep making separate variables. After a point of time, the code will just exit giving a segfault.
Now it is quite evident that if a variable has a local scope of usage, then it should be used as a local variable or else we would end up sizing more than required space by the code. Or in other words; in the block of code like declaring a function one can use the variable within the function itself and then end the usability. For this one doesn’t have to make any special adjustments with the code. Only assigning the value within the function with the local keyword will make it a local variable.
Another important pointer for all of you is that bash is whitespace allergic. What that means is that while assigning value to a variable, bash doesn’t like spaces in between. The main reason for this is that if there is space bash assumes the variable name to be a command itself and try to execute it. In one lakh of a possibility, the variable name might be a command but in other cases when it is not, the error would be thrown. Also, even if the variable name is a command the execution of that might not lead us to expected results.
The final nail in the coffin is how do we declare a variable to be local. We use the keyword local before the variable name and that makes it have local scope within the block of the code, i.e. code within the curly braces.
Once here, we will look at a real-life problem statement, and we would solve this by showing it as a part of an example. For the sake of simplicity, we have kept the features in the code minimalistic, but the thinking needed for the solving will be real-life one!
Example to Implement Bash Local Variables
Below are the examples of Bash Local Variables:
Code:
#!/bin/bash
echo "Learning scope of local and global variables"
function_localVar(){
echo "Within function function_localVar"
echo "Assign a variable with local keyword to variable name: varLocal"
local varLocal=27
echo "Assign a variable without local keyword to variable name: varLocal_wo"
varLocal_wo=9
echo "Printing within the function"
echo "Value assigned to one with local keyword varLocal = $varLocal and without local keyword varLocal_wo = $varLocal_wo"
echo "Exiting function_localVar"
}
glob_var=91
echo "Start of the code!"
echo "Global Variable = $glob_var"
echo "Local variable before calling function to one with local keyword varLocal = ** $varLocal ** and without local keyword varLocal_wo = ** $varLocal_wo **"
echo "Calling the function"
function_localVar
echo "Printing outside the function"
echo "Value assigned to one with local keyword varLocal = $varLocal and without local keyword varLocal_wo = $varLocal_wo"
Output:
In the above code, it is quite evident that local keyword makes the scope local and even if the variable is accessed outside the function where it is declared as local gives empty.
Conclusion
With coming to the end of this article we feel that one would have got enough exposure to the context of local variables in bash. Also, we would encourage you to try them to improve the self-learning through coding. With the example, we feel that you might have got an intuitive understanding of local variables and its use cases along with its scope! Also, you would have noticed that bash only allows a status of the execution to be sent within a function, unlike other programming languages where you can send the output of the function directly.
Recommended Articles
We hope that this EDUCBA information on “Bash Local Variables” was beneficial to you. You can view EDUCBA’s recommended articles for more information.