 Hello and welcome today. We're gonna be looking at shifting argument variables in bash So let's go ahead and just create a script. We'll call this args sure that sh Let's do args that sh you can call it whatever you want Of course, we're gonna start off with our shebang line laying our operating system No, that we're wanting to use the bash interpreter here now. Hopefully you already know a little bit about arguments being passed to a Script so for example if I say You entered dollar sign one and I'll make this script executable change mod plus X Args and then I can run that I do that and says you entered nothing, but if I type in John it will say John if I type in John Smith It just says John because we always said give it print out the first Argument if we come in here, we can say our dollar sign To and that would print out the first two variables And if I type in something else like Bob, it's not going to type that we can also come in here and say a strict which means To print out all the arguments so it doesn't matter how many things we type after here It's going to print them all also be aware that We're separating our arguments by spaces But if you want spaces in one of your arguments, you can put quotations or rep marks around it So if I said dollar sign one dollar sign two And we'll back up and we did something like this Bob Smith It's gonna say John Smith, but if I put quotation marks around this It's going to say John Smith and Bob because it's counting this as one argument Anyway, that was just a quick review on how arguments work and how you pass them one more thing I should show though because it's important to this tutorial is you can also Give it the pound sign or number sign or hashtag whatever you want to call it and then we'll give you the count of arguments So here we should get you entered what we didn't enter three, but we entered three arguments And again if I do quotation on that it's going to say two because it's seeing John Smith as one Okay, that was a quick review what we want to do is we want to shift through these so we want to do something It's kind of like well it is a while loop through each one of them So let's go into our script here and what we're going to do is we're going to type in while and Double parentheses dollar sign pound so we're going to go while this number we're going to loop through the number We're going to say do we're going to end that with a done and now it's going to loop through in here I'm going to say echo Hello dollar sign one and then we will type in shift So what this is going to do is it's going to echo out our first argument that we pass to it And then we'll shift that number and loop again And now what was our second argument will be our first argument and so forth and so on so now I Should be able to say something like John and Bob and we'll say hello John hello Bob. I can say Tom and I can come in here and I can say John Smith So say hello John Smith hello Bob hello Tom and we can say Jerry I'll say hello John. Hello. Hello John Smith. Hello Bob. Hello Tom. Hello Jerry. We can say Tom and Jerry so now we'll say hello John Smith. Hello Bob. Hello Tom and Jerry and so forth and so on so now you can see you could We're right now. We're just echoing out. Excuse me the responses But you could do a lot of stuff with the arguments looping through them this way So that's one way to loop through or in this case We're using a while loop and we're shifting each one of those arguments those variables being passed to our script Now one thing you want to note is if I don't give it any arguments We get no output which we probably do want some sort of output there So the user knows what to do so we'll go back into our script here And this is a good thing to do any time you have a script that arguments are being passed You want to check to make sure the proper arguments are being passed So I'm going to quickly here use one of my templates that I have That I created so here we are going to check for arguments So we're doing here is we're saying okay if the numbers remember that's the number of arguments is going to give you You know zero one two three four and here we're going to say If it's less than one so we're looking for at least one argument these spaces here are very important If you don't have these spaces you will get an error So if you try this and you get an error make sure you have a space after if and between the brackets here But we're checking to make sure we have at least one argument if There is not at least one argument Then we're going to echo out usage dollar sign zero as we talked about you know Dollar sign one is the first argument dollar sign zero is actually the name of your program So this you do this so in case your executables name is changed It's still going to give you you know if I name my program was my program And we put that there, but then someone changed the the bind or not binary But the name of the file it'll be wrong dollar sign zero We'll just give whatever the name of your executable file is even if it's been renamed So in this case we're going to say usage dollar sign zero one and we're going to exit with one meaning it failed Because our script did not run properly. We'll save and write that again I'm using Vim as my text there. You can use whatever text editor you want So here if I say John you'll print out John if I go through like this We'll print all this but if I give it without a argument. It's going to print out our usage message. It's saying usage args.sh names I go. Oh, okay now. I know I need to you know type in something like this and Lots of times. I'll give a usage and then I'll give an example to make it a little bit clearer. I'll say echo Example dollar sign zero John Bob I Like doing that making it very clear. So now if we run this we get that and I go okay I can just copy and paste this and my program should don't probably ah now I understand how this program works. So anyway, that is shifting through variable arguments in a bash cell Hope you found this useful if you did think about liking sharing subscribing commenting and also visiting my patreon page There's a link in the description, but it for your knowledge. It is Patreon.com forward slash metal x one thousand you can also go to films by Chris calm That's Chris of the K. There's link to that in the description there There's a support section where there's a link to my patreon page and also a link to paypal if you want to support me That way can't support me financially. That is fine But you know liking share sharing and subscribing helps a lot views help a lot and I appreciate it and as always I hope that you have a great day