Bash Scripting on Linux (The Complete Guide) Class 03 - Variables

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
thank you hello again everyone and welcome to class number three in my bash scripting series and in this particular class what we're going to do is take a look at variables what variables allow us to do is save information for later use and the best way for you to see exactly what I'm talking about is just to dive in and get started so let's go ahead and just do exactly that foreign [Music] lesson number three we're going to continue our bash scripting journey and add variables to our toolkit in the previous lesson we created a few scripts that executed simple commands but when we add variables to the mix we can start to differentiate our scripts let's begin so here in my terminal what I'm going to do is declare a variable right now I'm going to call it my name and I'm going to set it equal to J just like that and when it comes to bash declaring a variable is literally that easy you type whatever you want to name your variable and then you include the equal sign with no space and then in quotes you could put some text like this or even a number you don't have to actually include text so I'll press enter so what I've just done is I've created a variable named my name and I set it equal to J I did this right on the command line and not in our script file and I did that to underscore the fact that there's really not much difference between entering commands on the command line and adding commands to a script a script will contain commands that the underlying command line interpreter understands and commands on the command line can be used inside a script or as a standalone command interchangeably so I declared a variable but how do I reference it one way we could reference a variable is to print the contents of it and we could do that with the echo command in the previous lesson I used the echo command inside a script to print hello world to the screen here hello world is a string I have it in double quotes So I simply instructed Echo to Echo what I have in the quotes here but what I can also do is use Echo to print the contents of a variable as well now recall I named my variable my name so what I could do is echo my name just like that but this isn't going to work don't press enter just yet whenever we reference a variable in bash we have to include a dollar sign in front of the name of the variable otherwise it's not going to work exactly like you might expect so without the dollar sign let's just see what happens it literally echoed my name now here I didn't use double quotes but it still echoed a string to the Shell the double quotes aren't required they're just a good idea especially when you have special characters that you're going to Echo but this isn't really what I wanted I called the variable my name but when I Echo it it just prints my name not what the variable actually contains but what I wanted it to do was print the contents of that variable so in order to reference a variable what we have to do is include a dollar sign in front of the name of the variable just like that now when I press enter the results are going to be completely different it echoed my name that's what I set the variable my name to be equal to the name j and now that I've included a dollar sign in front of the variable name now bash understands that what Jay wants to do is actually print the contents of that variable instead of actually printing dollar sign my name it's going to print the contents of that variable so bash understands if it sees a dollar sign right in front of a name that you're talking about a variable that's what you're doing so it understands that and it's going to treat whatever you type there as a variable so if you haven't declared something yet it's empty because I've never actually created a variable named new variable so it doesn't exist in this case bash is treating it as an empty variable but my name that was declared I did set that equal to something so it actually contains information so you might be wondering then why does bash actually require a dollar sign in front of a variable name in order to reference that variable well don't follow along with this but just consider this example that I'm about to show you I'm going to create a variable named LS and I'm going to set that equal to the text hello again so I'll press enter and if I type the ls command the ls command still works even though I declared a variable that's also named LS it didn't interfere with the ls command and that's one of the reasons why there's a distinction between typing something like LS or some other string or typing a dollar sign followed by a string a dollar sign and string is a variable bash knows you're referencing a variable but the ls command I didn't include a dollar sign in front of it so bash understands the difference between the ls command and the ls variable because of that very reason so if I ran Echo and then dollar sign LS it prints hello again just like you see so essentially one of the reasons why we include a dollar sign in front of a variable name is that it actually helps us avoid name collisions it's also mandatory you don't really have a say in the matter if you want to reference a variable you have to use a dollar sign so it is what it is now there's one issue with variables that we want to keep in mind if I was to type exit to exit out of my terminal that's going to close the entire window and then I'll open another terminal window and here it is so if I Echo LS with the dollar sign nothing happens in fact that variable doesn't equal anything at all either so what happened here it's important to understand that when you declare a variable in bash it is tied to that session once you close the window the session's gone so any variables that you might have declared are wiped out they don't survive a log out log in or an exit of the shell they're going to be within that session only so the takeaway there is that variables by default they don't persist now let's return to our script and what I'm going to do is include the variables inside the script which is actually better because every time the script is run then it's going to declare the variables every single time so I won't need to do that manually so here's the script from the previous lesson I'm just going to wipe out everything here and let's go ahead and create our next example so the first variable that I'm going to create is the same one that I created at the beginning of this lesson my name I'm going to set that equal to J and right underneath that I'm going to type another variable I'll type my age and I will set that equal to 40. so what I'm going to show you right now is how to reference variables within your script and this will help you make the connection between variables that you set and some of the possibilities that you can use within scripts but even if you don't fully understand this concept don't worry about it it's going to get more clear as we go along anyway what I'm going to do is type Echo hello my name is dollar sign my name period and then an ending double quote there and on the next line another Echo statement I'm and then my age here's old so let's go ahead and run this I'll save it first again in the case of Nano it's Ctrl o and then enter and then Ctrl X to exit out let's go ahead and run it so as you can see here it typed the sentence hello my name is Jay I'm 40 years old so now let's go ahead and explain what we've done so far first I set up two variables right here my name and my age and I gave them values and what I want you to do now is pay special attention to the echo statements here there's a few things that you'll definitely want to memorize so right here I have Hello my name is but instead of actually typing my name I'm referencing the variable my name now this is within the double quotes So if you type double quotes then bash understands that if you have a variable within the string that it needs to print the contents of the variable and not the name of the variable now if I was to include single quotes instead and they have to match I'll save the file and close out yet again let's run it now it's saying hello my name is and it's actually printing the name of the variable it's not printing what the variable references it's actually printing the name of the variable and with that example you can immediately see the difference between single quotes and double quotes in an echo statement and this is actually why I used double quotes in this example because I wanted the contents of the variables to be printed now if I was to use single quotes down here that's not going to work very well why well we already have a single quote right here in the string so if I was to type a single quote at the beginning and change this over then this isn't going to run what bash is going to do is assume that this is what I want to Echo right here because it's what I have in quotes and the rest of it is not in quotes I do have an ending quote but bash is going to assume that this is the beginning of another quote so what I have here is an uneven number of quotes and this is a little complicated because I do in fact want the single quote to print and thankfully with the double quotes and the variable name it works out just fine as an aside single quotes would actually work if I went ahead and escaped out the single quote right here which essentially tells bash don't count this as part of the greater single quote string but that's not going to work well so don't try that we'll get into that later I just wanted to let you guys know what the importance is of double quotes here when we use double quotes in bash it's going to actually treat the variables inside these Echo statements to show what the variables equal rather than the names themselves now thankfully if I close out of this go ahead and save it and then I'll exit out I'll bring the terminal back what I could do is execute the script again and it still works so the takeaway here is that by adding variables to the bash script itself we don't have to declare them every single time we're about to run the script so that basically allows us to persist the variables but okay how do variables actually help us I mean we've just seen examples where we can print the contents of a variable but why exactly would we want to use a variable in production especially if we have a script that actually automates something instead of printing some silly text variables have many different use cases and you'll actually see quite a few of the use cases throughout this series but one of the major things that it helps us do is to avoid retyping and to illustrate that let's bring up our script again and what I'm going to do is just delete everything here if you're curious in Nano it's Ctrl K that deletes an entire line it's going to wipe out everything let's start over so what I'm going to do is write three Echo statements here now what I have here is another silly example but don't worry later on in this series we will definitely see some examples of some very useful scripts but let's just go ahead and roll with this for now what I have here are three Echo statements basically three things that I personally think are awesome now I'm not going to run this script I think by now you know exactly what's going to happen it's just going to Echo these three sentences right on the terminal no surprise that's exactly what's going to happen but what does this have to do with variables well if I wanted to change the word awesome to something else then I'm going to have to manually edit the word awesome in each of the three lines and well we don't like to do that when it comes to scripting we don't like to repeat ourselves as much as we can absolutely help it so what I'm going to do is create a variable named word and I'm going to set that equal to awesome then for each occurrence of awesome right here what I'll do is just change it to the variable so as you can see I've created a variable named word I set it to awesome and then I reference the variable in each of these three sentences so let's save the file let's give it a run and see what happens well I think you already know what's going to happen but I'll run it anyway as you can see it just prints the sentences originally like I had them but the cool thing about this is that if I wanted to go ahead and edit the word what I could do is change the word to something like fun for example and again I'll run the script and I was able to change each occurrence of that word in one shot now if this is a real script in a data center for example it could be doing something very important like backing up your server and maybe you have a variable that's the directory it's backing up or something like that in that case you could just change what's being backed up by updating the variable and that's a lot better than changing the path in every single occurrence manually at least with this method anytime you are actually referencing that variable you could change that variable equals in every single occurrence in one shot and that'll save you time so what I'll do is exit the script and give you another example of where variables might be useful and specifically another thing that we could do with variables is capture the output of a command consider this for example LS as we've already seen lists the storage but what if I want to grab the output right here and store that in a variable well what I could do is I could just copy everything here and put it in a variable but that's not what I'm going to do that's not very efficient we definitely don't want to do that what I could do instead is actually make a variable that's equal to the output of a command and here's a quick example of that I can create a variable named files and just like before I'm going to set it equal to something but instead of typing something out what I'm going to do instead is type dollar sign and then in parentheses I'm going to type LS just like that so I'll press enter and the variable is now declared nothing printed on the screen but we wouldn't expect there to be anything on the screen because well we wanted to create a variable and that's what we've done so if I Echo the contents of that variable as you can see it includes the output of the ls command how exactly did that work though now up until now we've been creating variables with double quotes manually typing inside the double quotes what we want the variable to equal in this case though I don't have double quotes instead I have a dollar sign and then a valid command in parentheses so what we have right here is a subshell and don't worry if this confuses you we'll get into this more later on in the series but a subshell allows you to execute a command in the background and that's great because maybe you want to grab the output of that command and store it somewhere so what this has done is send LS to the background that's why we didn't see the output and we created a variable named files that equals the output of the ls command and not to be outdone I can simply recreate the variable and this time I'll use PWD and if we Echo the variable then it now contains my current working directory my present working directory as the PWD command provides us so that's yet another example of how variables might be useful now to be fair at this point you may or may not be making a link to how this might be useful in practice we'll get to that but if nothing else you now know how to declare variables you can actually add a variable and set it equal to something like a string if you'd like or maybe you can make a variable that's equal to the output of another command because you want to use the output of that command later on and yet another example that might be useful is the date command and this is not specific to scripting at all but by executing the date command on a Linux system it'll give you well the date just like you see here if we were writing a script which I'll pull up right now let's see an example of using the date in a script so I'll create a variable and I'll call that variable now and I'm going to set the equal to the output of the date command so what this script will do is run the date command in the background and capture the output as a variable named now and then I'll Echo the system time and date is and underneath that I will Echo the contents of the variable that I named now and just like you'd expect it's going to print the system time and date is that's just a string nothing surprising there and then it's going to print the actual date and time now to be fair instead of this right here I could actually delete this foreign and then manually add the date Command right here and well the same thing is going to happen the output is exactly the same aside from the fact that the time and date is now a few seconds ahead but you get the idea so again this wasn't a practical example but what I did do is show you guys how to capture the output of a command save it in a variable and reference that right in your script and well this is something that we'll be doing again so you're getting your first taste of it right now it's a very simple concept but something that you will use again and again into the future now let's see another example I'll bring up the script and let's write something that's actually a little bit larger than the last one so again we have the numeral sign exclamation mark slash bin slash bash every single one of our scripts is going to have that as the first line going forward next what I'm going to do is create a variable named name and I'll type my name in there I'm going to create a variable named now and I'm going to set that equal to the output of the date command so next what I'm going to do is Echo a simple sentence hello in the name pretty straightforward so far and Echo the system time and date is Echo now and then on the final line of this example what I'm going to do is type Echo your username is dollar sign and then user in all caps but wait a minute we didn't declare a variable named user well don't worry about that quite yet let's save the script and let's go ahead and run it so check this out it says hello and then it shows my name the system time and date is it shows the system time and date and then it says your username is Jay but wait a minute I didn't declare that variable and it worked so depending on how long you've been working with Linux you either knew right away that was going to work or you assumed I was going to get an error or well see a blank variable but as you can see here the script worked exactly like I intended it so I think at this point you should understand what everything does up until this last line right here how did this actually work where did the user variable even come from well within your environment there are many default variables that are always declared and dollar sign user in all caps is one of those that variable is always going to contain the name of the user that you're currently logged in as it was already there I didn't need to declare it myself because well there's an environment inside your shell the environment contains a bunch of variables that are already there that are always there and user is just one of those variables and variables with a name that's all uppercase like you see here is commonly a system variable an environment variable something that's basically built in now there's nothing stopping you from creating a variable and creating that variable with a name or variable name that's in all caps you could definitely do that and it's not going to err out you won't have a problem it'll work just fine assuming of course that you actually update the variable name everywhere that it is but that's besides the point lowercase and uppercase variable names have an important distinction and this is why you shouldn't create your own variables that have names that are all in uppercase it's actually proper etiquette to use lowercase names for all of your variables so that way if somebody sees a variable being referenced that's in all caps then that person will understand that you are referencing an environment variable that's in the underlying system and it's not a variable that you created yourself again there's nothing stopping you from creating your own variables and uppercasing everything I just recommend that you don't do that it's probably better just to go along with the best practice and name your variables with lowercase names and that way everybody will know when you are referencing an uppercase variable then you are referring to an environment variable so I'll exit out of this may as well save the changes anyway if you're curious which variables which environment variables you have within your session you could find out by typing EnV just like that and as you can see here I have a ton of variables that are already declared I didn't declare any of these variables right here the underlying system actually declared these for me so if I scroll through the list here I should be able to find the user variable so we have username we have home and right here we have user and that's where I got it from as you can see it was declared right here in the environment we have a bunch more so for example I have term I could Echo the contents of that we also had one for home so if I exit out of here you can see what that equals just like within the script we have user and there's a bunch more but anyway what I'm going to do is close this lesson right here that was a lot of information basically the takeaway is that variables are great because they enable us to save information to be used later variables also protect us from retyping the same thing over and over again because anytime you are referencing the same thing repeatedly it's almost if not always a great idea to use a variable instead so that way you only need to edit that one variable in that one line and everywhere else that variable is being referenced will automatically work and that just helps you avoid human error foreign this particular lesson and if you did please click that like button I would really appreciate that and the next video in this series is already uploaded so feel free to check that out whenever you're ready to do so anyway in the meantime thank you so much for checking out this video I really appreciate it and I'll see you in the next video [Music] foreign
Info
Channel: Learn Linux TV
Views: 117,185
Rating: undefined out of 5
Keywords: Linux, gnu/linux, LearnLinuxTV, Learn Linux TV, Learn Linux, Linux Training, Linux Tutorials, bash scripting, bash script, bash for beginners, shell scripting, linux shell, bash scripting tutorial for beginners, bash scripting tutorial, shell scripting linux, shell scripting tutorial for beginners, shell scripting tutorial, hacker, hackers, howto, how to, tutorial, guide, bash, bash scripting linux, bash scripting basics, shell scripting basics, linux for beginners 2022
Id: uQE_4Q-HZZw
Channel Id: undefined
Length: 24min 46sec (1486 seconds)
Published: Mon Nov 28 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.