Learn CentOS 16 - Shell Variables

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys how's it going and welcome back to my channel so I'm breaking from the normal format for a few videos or maybe more than a few videos because at the time I'm recording this video its isolation time throughout the world and you know all the salons are closed I look like a caveman and I don't even have my recording equipment set up in the new location where I'm recording just yet but I definitely wanted to make some new content for you guys and keep this series going so that's what I'm going to do now in this video I want to talk a little bit about shell variables now I've mentioned shell variables several times throughout this series but so far we have not had a dedicated video on that subject so what is a shell variable well if you've ever used a programming language you know exactly what a variable is and when we're talking about bash basically the default shell on most distributions of Linux the concept of variables is exactly the same we could set basically a variable we could create a variable and set it equal to something and then we can recall that information later now the best way to illustrate this is to well show you guys an example so let's go ahead and get to it so what I'm going to do is create a variable right now and then I'm going to explain exactly how it works so I'm going to create a variable named hello and I'm going to set it equal to a string so for a string we want double quotes more on that later and then I'm going to go ahead and type a message so I just wrote sent OS is awesome well because it is so I'll press ENTER and well I guess it looks like nothing happened right well it didn't air so I know that this command was successful so what we have done is we have created a variable name hello and we've set it equal to ascent OS is awesome so that's all well and good but what can we actually do with that so what I'm going to do right now is use the echo command and I'm going to echo that variable name right here but it didn't work well actually when you want to read the contents of a shell variable you need to actually put the dollar sign symbol like that in front of it so it knows that you are referring to an actual variable otherwise a command like echo is just going to echo whatever you type specifically what we want to do is see what the variable is equal to so we need the dollar sign for that we didn't need the dollar sign when we set the variable we need it though when we actually refer to the variable so if I press ENTER here we can see that it actually printed the message so I mentioned earlier when I created the variable that I was going to be creating a string now this is not a tutorial series on programming although you know when it comes to bash it basically is kind of like a programming language because you could do a lot of the same things in bash that you can do in a proper programming language and you can of course create variables and variables can be strings which basically means a string of characters and a variable can even be a simple number now you might be wondering you know why should I care about variables why should I learn this and if you're not going into programming you're especially wondering why you should be learning this but as you get deeper and deeper into Linux administration you'll find yourself writing scripts to automate processes and things that you do on a daily basis that's just the natural progression and when you do that you'll find yourself needing to refer to things later and needing to save things for later use and variables will help you do just that now what I'm going to do right now is give you an example that's somewhat contrived but it's going to show you basically that you can use variables in your daily command so what I'm going to do is create a variable I'm just going to call it my Derr short for directory obviously and you know one thing you might be wondering is why am I always creating variables in all caps would it still work if I did it in lowercase well yeah it would it's just a very common thing to do in the Linux community not everyone does it so you will see some scripts out there where that's not the case there's nobody that's going to scream at you if you do it you know with lowercase although let's be honest you know some people are more particular than others but the fact of the matter is it's just a common thing to create your variable names in all caps but ultimately it's up to you whether or not you want to follow that so what I'm going to do is just create a variable again my derp and I'm going to set it equal to something and what I'm going to do is set it equal to SC SSH basically like a path right so obviously this example is going to be a bit lame because it's probably much easier just to CD or type that directory path right there than it is to create a variable but let's just go ahead and do it so I created the variable and then what we can do is PWD you already know that we are in our home directory right now so what I'm going to do is CD into you guessed it my der so what's going to happen well you can see right away that the path that we are basically working in has changed and if I do PWD then it's moved us into Etsy SSH so right away you can see a potential use case here if you are going to constantly be referring to a very specific directory and maybe it's longer than you know slash Etsy slash SSH maybe you have a very long path you might want to create a variable for it and then basically you could just CD into that directory you can LS because again I could just run any command here against the variable since it contains a path it's just a string that has a path I could use LS and I can refer to this anytime I want to without having to type it every time and then when you get into scripting if that's something that you get into then if you need to set a direct in there and you need to refer to it multiple times throughout the script you could basically just put the directory as a variable and then at the top of the script you simply just change what the variable equals basically if you need to change the path so that essentially prevents you from doing a find and replace so what I'm going to do is change back to my home directory and wait wait a minute how did I do that CD dollar sign home well that's clearly a variable because it has a dollar sign in there and I was able to use this CD command against it but how exactly did that work I didn't set that at least you didn't see me do it I didn't do it off camera I promise so actually some variables are created for us on the shell that we can refer to even though we didn't create those variables ourselves so what I'm going to do is run the env command short for environment obviously and I'll press ENTER and well look at this we have a ton of variables these are all variables and you can see they're all in caps here and we have all kinds of things that I didn't set any of these so we can see already right here that the home variable is set to my home directory so some of these variables are just part of your environment they're just part of your session they're here by default it basically goes beyond the scope of this tutorial this series you go down deep in the weeds of how these variables got set but the important thing to take away here is that there are some variables that that are set by default you can override any of these if you'd like but these variables are here and you can use the env command to see what session variables you actually have that are being applied to your session here and we can see like my username so I could refer to that anytime and even shorter just simply user I have my desktop session so if I need to run a query on someone's desktop session I could query that variable and I can see that the person in my case I am logged into gnome here so there's all kinds of information that you can get just by looking at the environment and these are variables that are created for you that you can then refer to later this video is sponsored by Linode my cloud infrastructure provider for over two years Linode provides linux servers that make it easy and affordable to host your own app site or service live in the cloud whether you're a linux power user or just starting out you can use Linode you can start from scratch and fully customize your server for any application or use Leonard's one-click apps to deploy game servers WordPress sites personal VPNs and much more you can even upload and run your own image servers can be easily scaled up or down so you only pay for what you need and regular backups are also available so you'll never lose your work best of all Linode comes with 24/7 support that is 100% managed by humans by phone support or support ticket to get $20 in free credit when you create your new Linode account sign up at Leno comm slash learn Linux TV the link is in the description I'd like to thank Linode for not only being awesome but also for their continued support of my channel I really appreciate it now let's get back to the video now there is a variable that I want to show you that is kind of important here I guess it might be somewhat of a stretch to you know call it a variable although it kind of is so first of all I'm going to run the LS command no surprise there just list the storage you've seen this probably a thousand times by now but what I want to do right now is echo the contents of another variable and what I'm going to echo the contents of is this variable right here dollar sign question mark so I'll press ENTER and I get 0 what the heck is that so just to illustrate the difference what I'm going to do is type LS and the directory that I want to list the storage of is this directory does not exist because it doesn't a press Enter and it aired out we probably expected that to happen because there is no directory or a series of directories that fits that path there so if I echo that again we got a two so okay so what's going on here so when you echo dollar sign question mark you are trying to get the exit code of the previous command a zero means a success so what I did here was I ran the LS command and then it showed me basically the contents of my current working directory just like we would expect it to and it was able to do that I issued the command it gave me the results and then it returned so that was a successful command it LS did his job it showed me the directories so we're good but when I did this command right here I ran LS against this contrived example directory path right here it's telling me well I can't access that no such file or directory that is an error and it basically echoed out a - right here so basically zero is success and anything that's not zero is a failure so what you can glean from this is that echoing dollar sign question mark will tell you whether or not the previous command was successful now you might be thinking that this is kind of useless because we know full well that this command definitely was not successful because we got an error so we know it failed right but if you are running in a script then you might want to run or create an if statement that does something different depending on the exit code so if something fails maybe you want to send an email to your administration team to let them know that there is a failure so if you run a command and then it fails it check for the you know the air the exit code there and then if the exit code is not zero send the email let people know that it failed otherwise if it's zero okay everything's fine we can proceed and when you are running a script you generally don't have someone that is sitting in front of a terminal I mean you could but a lot of times you have a script that's running in the background during a previously scheduled time and you know no one's sitting there so you want some kind of automation and sometimes that does require checking the exit code so that's why you would want to was that but I wanted to basically point out that that does indeed exist and another thing that I want to do as well is just show you guys so I basically created some variables throughout this particular video and one of them was my derp right so I need to echo that to show you guys that it does still exist and it does so then what I'm going to do is open a new terminal and what I'm going to do is echo my dirt well it doesn't give me any output but it worked here right we've probably already figured out where I'm going with this when you close your terminal session you start a different terminal session then that means that that variable is gone if I was to close this session right here of course that variable will be gone here but it's not a global variable it's a local variable this my dur variable is just local to this particular session and it doesn't carry over to other sessions so that's something that you need to remember and you can also basically edit your dot bash our C file if you'd like to create a variable that's going to stay persistent and recreate itself when you open up a new shell so that's one way we can go about that but of course like most things in Linux you know there's multiple ways that we can go about this so I know that was a shorter video guys I just feel like there's some concepts that I should have gone over closer to the beginning of the series and I just figured I would spend the next several videos or so just going through all the smaller components that I should have covered by now just to make sure that your knowledge of Sint OS is complete so I'll go ahead and see you in the next video I'll get that uploaded as soon as I possibly can and once I have that up here I will see you there so stay tuned you [Music]
Info
Channel: Learn Linux TV
Views: 5,282
Rating: undefined out of 5
Keywords: LearnLinux, Linux, Tutorial, Review, Howto, Guide, Distribution, Distro, Learn Linux, operating system, os, open-source, open source, gnu/linux, centos, learn centos, centos tutorial, centos 8, rhel, shell variables, shell, variables, scripting, exit code, shell scripting, shell script, linux tutorial, how to, learning linux, shell programming, bash scripting, learning linux for beginners, tutorial centos 8, linux centos tutorial, centos server tutorial
Id: YMCCeET9q6o
Channel Id: undefined
Length: 15min 49sec (949 seconds)
Published: Wed May 27 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.