Shell Scripting Tutorial for Beginners 26 - Readonly command


hey guys welcome to the next blog on shell scripting tutorial for beginners in this blog we will learn how to use read-only command in shell scripting now read-only command can be used with variables and functions and by the name itself we can understand that it can be used to make variables or functions read-only so they cannot be overwritten so let's take an example so first of all we will take a variable for example var and then we will assign some value to it so for example 31 right and now we can use a read-only command on this variable so just write read-only and then the name of the variable so read-only space bar for example ok and till now everything is ok but now after this point whenever we want to reset a reassign some value to the variable same variable var then let's see what happens so we will just run our script for example and you can see on line number 7 var read only variable so it gives us a warning that war is a read only variable and we cannot change the value of it so let's print the value of wire for example so I'm going to just echo and then bar is equal to let's give it something like this and then let's print war so let's print the value of war and the value of war is 31 so even though we have reassigned a value of var to 50 but the var remains same as this one which is 31 because we have made this war variable read-only and we cannot reassign any value to war because it's a read-only very now and you will also see this warning so you will be able to understand that this is our read only variable in a similar way the functions can also be made read-only so let's take an example so I'm going to create a hello function for example and then I will just print hello world using echo for example hello world okay so everything is okay right now and whenever we call this function hello and let's run the script once again ad prints hello world everything is okay right now if we want to make this function read-only then we use a read-only first of all the read-only keyword which is this one right before the function name but this will not make your function read-only for functions you need to use minus F flag okay so you need to use this minus F option in order to make a function read-only right and now whenever you want to overwrite your functions for example I will once again create a hello function and I will just echo this time hello world again right something like this and let's see what happens so I am going to run the script and you can see this warning at line 19 and it says hello read-only function right so from here we know that this error is coming from the line 19 right it will give you the place where it's coming from and it says that the function is read on this we cannot overwrite this function okay so read-only command can be used to you know create a variable or a function read-only that means we cannot you know reassign the value of variable in case of variables and we cannot overwrite a function in okay so function if the function is read-only function so sometimes we need these kind of read-only variables and functions in our script and this is how we can achieve them okay now for example we just write the keyword read-only here at the last okay so what happens when we write our read-only keyword only and nothing in front of it so let's run the script and let's see what happens and you can see here after this warning you will be able to see all the variables which are read-only by default right - let me make it a little bit clearer I'm going to just you know remove everything and I will just use a keyword read-only and let me clear the terminal and run the script once again and it gives us the list of all read-only built-in variables which are listed here you can see that that bash opts are read-only and we can see bash PID is read-only bash version info is read-only UID is read-only right so all the the you know variables are built in and they are read-only you can also use the option - P with this thing and this will give you almost the same result okay so if you want to see the list of all read-only variables yet then you can use read-only only like this or with the flag P okay and if you want to see all the read-only functions then you just need to use - app flags okay so once again let me clear the terminal and let me run the script and right now there is no read-only function right and we can create this kind of function so once again I'm going to create the hello world and I make this function read only something like this right and then I just write read only minus F and let's see what happens so now it gives us this function which is read only okay so minus f is for functions so if you write read only minus F and nothing in front of it it will give you the list of all the function which are read-only okay so minus P for variables and minus F for functions so I hope you've enjoyed this blog         

Post a Comment

Previous Post Next Post

Recent in Technology News