hello guys welcome to the next blog on shell scripting to doll for beginners in the last Blog we have seen how we can read parameters in our script now in this Blog I am going to show you how we can pass argument to a bash script so whenever you start the script you can pass some argument with it so let's get started so let's discuss the first way of passing the argument and how we can parse those arguments so we will just write echo and whenever you pass an argument from our bash script they are you know stored into a default argument which are like this so the first you know argument which you pass in an array will be stored in dollar one variable then the second argument which you pass into you know a bash script will be saved in dollar two and so on right so for example if you want to pass three arguments then it will be stored in dollar one variable then dollar two variable and dollar three variables consecutively right so let's pass some arguments and let's print those arguments also so what I'm going to do is I'm going to take single quote and I will just write echo dollar one dollar two and dollar tree okay so let's start our script and let's see how they are passed okay so I'm going to start my script which is hello dot Sh and here with the script I can pass some arguments so for example you want to send some name to a script whenever you start the script so for example I want to send three names for example mark Tom and John for example and I will just press ENTER and you can see they are printed so the first you know name goes into this variable second name goes into the second variable and third name goes into third variable by default right now where does this name goes so the zeroth variable whenever for example you write something like this dollars zero then you will be able to see this script name also okay so let's run the script once again and let's see what happens so the same script and then with the same argument press Enter and you can see the name of your shell script also comes so this is the zeroth variables you can say and then the actual variables which you actually pass in your script as an argument starts from variable one and then so on variable two and variable three okay so this is how you can pass the argument to a back script now there is a second way of parsing the argument whatever you pass into your script as an array okay so how we can pass those argument into an array and then use them so what we can do is we can declare any variable name for example odds or arguments whatever name you want to give here and then there is a default variable in which these are you know these argument goes as an array and to pass death star a you just need to write bracket and in double quotes dollar at the rate so this dollar at the rate you know stores your argument as in array and to pass them you just need to declare a variable and then you know assign those argument which are stored in this dollar at the rate into this argument right and now we can echo those arguments so just going to copy the psycho command and then this argh at index for example one let's print out the argument at index one and in the last blog i've said you how to you know print and re-index right so you write dollar symbol and then the name of your array inside the curly brackets so inside the curly brackets name of the array and then whatever index you want to see so for example I want to see index one here right and then same way you can do with the second and the third argument at index two and the third argument and let's say if you also want to pass the argument which is at index zero so I'm going to just write our index 0 okay and let's run the script once again and let's see what happens ok so I'm going to just you know run the same script with the same arguments and press ENTER and you can see those argument which we have passed first so first of all they are printed using this echo command and then in the second line they are printed using this echo command so the important thing to notice here if whenever you pass the argument as an array the first argument which you pass here will be assigned to the array at index 0 right so these default variable starts from 0 1 2 3 and 0 is the variable for the script name right but if you assign them as an array the 0th index will be for the first argument right and then first index will be for the second argument and the third index second index will be for the third argument so if you delete this also then the result will be the same so let's run the script once again with the same argument and the result will be the same right now for example if you want to print all the argument at once you can do that also so you can just write echo and then use this default variable at the rate which we have used to convert this argument to an RA right so if you use echo and then this dollar symbol and then this default variable at the rate then all the arguments will be printed using this variable so let me comment this code and now let me print those argument with this default variable so let's run the script once again and the result will be same so all those argument will be saved inside this default variable okay now sometimes you want to know the number of argument which you have passed into the script so to print the number of argument which are passed into the script you can just write echo and there is a default variable once again which is hash then it's going to you know print the number of argument which are passed so in our case we are passing 3 arguments so it's going to print 3 here so let's run the script once again and let's see what's the result so you can see the result is 3 here okay so this dollar and the default variable hash is going to print the number of argument passed to the bash script okay so these are some of the ways you can you know pass the arguments to your bash script I hope you've enjoyed this blog/article
Post a Comment