Shell Scripting - Variables

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Laughter] hello everyone and welcome back to the shell scripting course in this video we are going to be looking at variables now as you probably know with any programming language or scripting language for that matter variables are extremely important for the store of data data that can then later be used to perform uh other functions or can be used for output and various other various other functions that being said i've already created a variables dot shell file right here and it's saved on my desktop let me just show you that and i'll be continuing with my default environment that i had in the first video where i'm going to be using sublime text okay so i have that opened up and i've already uh i've already initiated here the interpreter that i'm going to be using okay so without further ado let's get started now the most important thing to understand first of all is if you do not know what a variable is a variable is simply a storage location with a specific name all right so essentially you're assigning a value uh you're assigning a value to a name of the the name being the variable itself uh and again the variable holds a certain value and we'll talk about data types later but for now uh let's just understand what's going on all right so i've already created the the shelf uh the shell script so you can do that as well and make sure to specify your interpreter it's always good practice so uh when we talk about when we talk about variables now of course if you have done any other programming language it may seem like second nature but with uh with the bash it's really slightly different so what if i wanted to create a simple variable as you would called name and then i would specify the value of name to be something like my name so i can say alexis right now the interesting thing about this is when we talk about the syntax of uh the uh the bash or the shell scripting uh this is not considered good practice in the sense that the variable name should always be in uppercase okay now when i when i say this of course people might be a bit confused but let me explain so we have the variable name and then we have the value quite simple now the rules are for a variable in shell scripting is that you cannot have any spaces after the initialization of the variable name and the value so that means no spaces after the before or after the equal sign okay as you can see here and the variable names have to be in uppercase now i'll get to uh to the different rules uh in regards to giving variables names but for now just remember those ones so let's start off by creating a very simple variable and again we'll stick to what i just used and we'll say name is equal to alexis all right now the value can be whatever you want to specify remember work we're using a string here so make sure to specify that now uh what can we do with this well we can save this and essentially this if we run this nothing really special will happen as you probably would have guessed uh so we now need to uh essentially find a way to use the variable or the value of the variable and how do we do that the easiest way is to print the variable out so how do you print out the variable now this is something again that's slightly different from what you'd expect in regards to using it with concatenation let me explain so to use a variable you need to insert a dollar sign before the variable name let me show you how this is done so let's say we wanted to print out my name all right so i would say echo and i would specify the quotation marks and then in here would say my name is and then i'll specify the variable with any other pro programming language i would just specify the variable there or use concatenation i would use the plus uh you know i would use the concatenation operators but for this case i have to specify a dollar sign and then the variable name as you can see right here the variable name in this case is variable is a name and then once i essentially uh run this script it will print out my name is and then it will count this space right here and then it'll it'll understand that i'm specifying this variable and it will print that out in one uh in one command now to test this out what i'm going to do is uh for some reason my sublime text isn't responding hopefully that saves saves everything yeah let me just save that up and again i'm going to have to give it permissions so let me just go into my desktop here and um what i'm going to do is um we're just going to chmod plus x and we're going to modify the permissions for variables and we are going to launch your variables dot sh and as you can see my name is alexis now again these are really really simple scripts but understanding the basics is very very important in the next video we'll be looking at creating our first well i would say quite advanced script where i'll teach you the rest and then you can then fill in the gaps and i'll be explaining everything as we move along all right so that is how to essentially print out a variable with its value of course now let's look at something uh interesting when we talk about variables and how they can be used uh the other way of displaying or printing out a variable is by using curly braces and also the dollar sign is involved now what am i talking about again let's say we stick with the variable name right here and let's say i wanted to print this out but i wanted to concatenate it with a sentence so let let me create another variable okay so let's say i create a value variable called spot right and spot is equal to um let's say i just call it foot all right now you you'll understand what i mean in a second so we said that the value of spot is equals to foot and now i want to print out a statement that will utilize this variable but i want to utilize it in conjunction with another word how do i do this well i can simply print out so i can say um the most popular sport is and then i would specify the variable uh right here now the the reason we use curly braces okay let me let me print it out and then i'll i'll explain exactly what what and why we are doing it all right so let me just correct that silly mistake there so the most popular spot is and then i declare the variable but in this case you're going to see what's different so i say in here and then i declare the variable name so spot and i close that up and then i just end this with ball okay now you can pretty much understand what's going on here all right i'm essentially printing out uh i'm printing out a statement here that is going to utilize this variable but in conjunction with what follows after now if i did it with the first method where i only use the dollar sign that is going to be considered a specific piece of text that is to be printed separately from any other piece of text or data that you want to print out now let me explain what this is so let's save this and i'm just going to go back into my terminal here and i'll launch that and as you can see it concatenated it perfectly so instead of printing out foot uh the value of the spot on its own it conjoined it with the uh the text or the data that followed after and that is because we specified the curly braces so you can see how important the curly braces are when it comes down to concatenating data together with other piece of or just simply making sense of the data if you are to print it out now we look at how this can be used in much more advanced um types of scripts and after this we'll be looking at more practical approaches but i wanted to explain the basics now i also mentioned that i'm going to be talking about variable names so let me talk about that for a second all right so variable names what's what's the big deal about them well in shell scripting something there are a few things that are slightly different so for example uh the standard rules apply a variable can have a letter it can have letters words uh names uh it can have numbers underscores whatever but there are rules as to how a a variable is supposed to be so uh let me show you how uh the different combinations so let's say i wanted to say um i wanted to create a variable and i wanted to say uh this is let's say student i can sorry make sure it is in uppercase again that's something i cannot stress enough so let's say student uh underscore 0 1 is going to be equal to uh john all right that's that's an example of a good variable um let's see the wrong way of doing it is to start the variable name with a a number or a digit that is completely wrong and again likewise for the underscore remember it can contain all of these three components it can contain letters and underscore and digits but they have to be in in a way that uh again the the the interpreter can understand and this is the only way the the wrong way of doing this would it would be me saying uh 0 1 and as you can see something weird has happened there so 0 1 student is equal to let's see we can just say alex right something something really simple like that now that is the wrong way of declaring a variable name now of course we'll get to see why and how this is when we look at these scripts that really do something uh but that's what i really wanted to demonstrate when we talk about variables so that's gonna be it for this video uh if you like this video please leave a like down below uh if you have any questions or suggestions let me know in the comments section on my social networks on my website and i'll be seeing you in the next video
Info
Channel: HackerSploit
Views: 65,754
Rating: undefined out of 5
Keywords: hackersploit, hacker exploit, shell scripting, shell scripting for beginners, shell scripting in linux, shell scripting tutorials, shell scripting tutorial edureka, shell scripting windows, shell scripting projects, shell scripting examples, shell scripting tutorial for beginners in bangla, shell scripting crash course, shell scripting tutorial for beginners 1, linux, shell, kali linux, hacking, bash, os, software
Id: WhqfbwQbWVE
Channel Id: undefined
Length: 10min 16sec (616 seconds)
Published: Mon Aug 20 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.