hey guys welcome to the next blog on shell scripting tutorial for beginners and in this blog we will talk about local variables now by default every variable which you declare in your script is a global variable and that means it can be accessed from anywhere in your script now let me give you an example and then we will understand why do we need a local variable and how to use them so this print function I have shown you in the last blog that you can declare a function like this and this is a function print which takes an argument and whenever we provide an argument for example we call this print function like this and we provide the first argument this way it will be assigned to this dollar one we have seen in the last blog right now let's say this function we need to use to print a name so I'm going to declare a variable called name here and I'm going to assign the value of for this first argument like this here and here we will just print the value of the name right so we can just write the name is and then the variable name which is dollar name here right and let's run the script and let's give some name here so print for example the name is Max here right so I'm going to just run the script and let's see what happens so it prints the name is Max which is correct right because this name is assigned to this variable and then this first argument is assigned to the variable name and we are printing the value of name now I said previously that all variables are global in shell script so the name can be declared here also and then we can assign some value to it so let's say now we assign our name of where value Tom to this name variable right and then we can just write echo here and then we can just say the name is and once again dollar name right so let's run the script once again now and let's see what happens so first it says the name is Tom because we have assigned the value of a name first of all Tom so this is the function declaration right and until and unless did you call a function it will sit like this right so the shell script execution start from here right and we declare a variable called name and then we assign some value to it which is Tom here and then first of all we are printing the value of a name which is we which is assigned to the name so it will print it will be printed from this echo command and then our print function will be called and then using this name variable this echo command will be executed which is printing the second line of code which is the name is Max and then our default echo value which is foo is printed here right
so what it tells us that this name is a global variable and we can access it from anywhere in the script but sometimes we want that our variable which we define in a function should remain a local variable so it should not be changed outside of the function and that's where the local command comes in so there is a command called local and whenever you add this command then your local variable your variable becomes local and it can be used inside a function only right so we can just declare this variable name something like this and then we can say local we add a local keyboard before it so let me just remove this local keyword for now and let me add once again the name is here so I'm printing the name is after the print function is called and before the print function is called right so I will just say the name is this and then we can just write colon before and colon after right and let's see what happens here before adding the local keyword right so I'm going to just run the script once again and you can see that once again the Tom value is assigned to the name so before this function is called the value which is assigned to name is Tom which is correct but after this function print is called now the value max is assigned to the name variable now so it prints the name is max from diff echo command and here this command prints the value the name is max once again so the you know variable which is name now possesses the value max right so this is little bit confusing right because our we expect that this should remain Tom because this is outside our function right so this name is local to this function and we expect that it should not change the value of this global name right so here we want the name is Tom because originally this is a global variable and we have assigned the value of Tom here and after this function is called we expect this this variable possess the value Tom and not max but what's happening here after this function is called the value Max is assigned to this and and then it's printing the max name here using this echo command which we don't want and that's why we use the local variable for example we just write locally here and once again when we call the script or run the script now see what happens so now whenever you add this keyword local in front of your variable then your variable is local to this function and it cannot be accessed outside the function so this is just a local execution of codes and it will not affect the global variable so now what happens we are in this variable name is different from the this variable local name right so first of all we have assigned the value term to the name and it will print the name is Tom using this before right so this is the echo command which is printing the name is Tom here then we are calling the print function I print the name is Max here because we have assigned the value to this local variable Max but not the global variable name right so it prints the name is max but once again when we call our name variable then it prints the name is Tom once again which we are expect right so this global variable is now different from this local variable because this local variable is you know only accessible by this print function not from somewhere else from this script so this is how you can use local variables and this is the concept behind using the local variable so I hope you understood the basic concept I hope you enjoyed this blog
Post a Comment