 Hello, and welcome to this tutorial by filmsbychrist.com. That's Chris at the K. There's a link in the description. I am Chris, and today we're gonna be looking at some very basic beginner shell commands for the bash shell. And we're gonna put that into a script. My channel mainly talks about free open source software and scripting and programming. And mainly I look at bash scripts and commands. And I try to every year do some sort of beginner type series. And I've done, obviously those in the past. And I'm gonna try to link to those at the end because this year I'm going to go through the basic commands really fast, where I've gone through them more thoroughly in the past. And again, I'll try to link to that at the end of the video to a series where it kinda goes over the stuff we're gonna go over today, but at a slower pace. But I wanna show you a bunch of basic commands, create a simple script, and the next video will probably go more in depth and create something more useful. But open up your shell on whatever links distribution you're using, your terminal, and the shell that you're running, you want to make sure it's the bash shell, which is the default on most link distributions. But it may not be the bash shell. And there are a lot of the shells. In fact, when I open up the terminal on my computer, I usually use Z shell for when I'm running commands. But my scripts are bash shells since that's more commonly on other people's systems. And although these different types of shells are very similar, there are things that might act differently in the tutorial I'm going over today. So make sure you're running the bash shell. So the first command we're gonna do is echo. And if we do echo dollar sign zero and hit enter, it's gonna tell you what shell you're running. So do that, and hopefully it says bash. If not, just type bash and hit enter. And hopefully it will open up bash and you'll be in a bash shell. So the echo command, what does that do? Well, the echo command will echo out whatever you write. So I can echo hello world. And it will print out on screen, hello world. Now, normally it's good practice in my opinion to put quotation marks around this because things get a little funky if you don't. So I try to always put quotation marks around what I'm typing. So echo hello world, either with or without the quotation marks in this particular case, I'll put the same thing. But let's say I'm really excited. I can say echo hello world with an exclamation mark. Yay, hello world. Okay, what if I was really, really excited? I'm gonna do two exclamation marks. Okay, something weird happened. And that's because double exclamation marks in bash are considered a special character. And what it's going to do is actually, if you do exclamation mark, exclamation mark, well, let me go back and run this command just so it's a little clearer. Now if I do just do exclamation mark, exclamation mark on my command, it's going to run the previous command. So I've hit enter. It just runs the previous command, which is hello world. So it's printing out the command and then it's running it as hello world. So that's what exclamation mark, exclamation mark does. It's a special character. So how do we handle special characters? There's a few ways to do this. If I wanted to do hello world with two exclamation marks, what I can do is instead of double quotation marks, I can do single quotation marks, which is the apostrophe on your keyboard. So if I do that now, it's going to say basically, look at any character in here and print it literally what it is. If it's a special character, just print it as it is, not as what the special character represents. Let's try to make an example of this a little clearer. We're going to create a variable. We're gonna create a variable called name. I'm gonna say equals and then in quotation marks, I'm gonna say Chris. Make sure you don't have any spaces around the equal sign and we'll hit enter. Now anytime I type dollar sign name, it's gonna use the variable instead of the word dollar sign name. So I can say echo dollar sign name and it prints out Chris. I can also say name is dollar sign name and now it says name is Chris. But if I wanted to say dollar sign name is Chris, well that's not gonna work. If I say dollar sign name is dollar sign name, it's gonna print Chris is Chris. If I do single quotations around that, well now it's going to say dollar sign name is dollar sign name and that's still not what we want. So how do we do this to where it says dollar sign name is whatever the value of that is, in this case, Chris? Well, we can do backslash. Backslash is a character that says print the next character if it's a special character literally as what it is, not what it represents. So I can do double quotations and backslash before that first dollar sign, now it says dollar sign name is Chris. Great, so we've learned how to echo things to the screen. We've learned a couple different ways to handle special characters, which there are more than what I just showed you, but these are some common ones. So now we also looked at how to create a variable and creating the variables is very useful in your script but lots of times we wanna get information from the user and put that into a variable. So I'm gonna hit control L here to clear the screen. If I didn't say that earlier, that's one way to clear the screen. You can also, like if I was to do this time, you can type in clear to clear the screen or you can hit control L in most cases to clear the screen. But to get user input and put that into a variable, we're gonna use the read command. So we're gonna say read and then we're gonna put what our variable is called. It can be pretty much anything you want but stick to regular characters. You don't want any spaces or special characters. So in this case, we're just gonna do name. So we're gonna hit enter. Now it's going to wait for the user to type something and hit enter. So in this case, I'll just say Bob. So it changes because we've already set name but we're gonna reset it now to Bob. So now I hit enter. So it read the user input and now we can say echo name is name and now it's gonna say name is Bob. So we've read the user input and put it into that variable name. But how will you want the user to know what to type in there? Well, you could in your script use the echo command say enter your name and then you can say read name but read in bash at least has a built-in functionality. And I say in bash because other shells, this functionality doesn't work at least not like this. I can do dash P for prints. It's gonna print a message here. I'm gonna say, what is your name? Question mark and I'm gonna put a little space there just for, because I think it looks better. Now read is going to print that message and then wait for the user input and I'm gonna say John in this case. And now I can say name is John or I can say hello name. And again, we can run that command name. What is your name? Name and this time we'll say Tom and now if we run hello dollar sign name it says hello Tom. Let's go ahead and take what we've learned here so far and put that into a script. I'm gonna use Vim as my text editor. Vim is sometimes considered scary to new users but you can use any text editor you want. Nano, you can use a graphical one like g-edit but make sure it's a text editor, not a word processor. You don't wanna use Libre offices right or something along those lines. Make sure it's just a text editor, not a word processor. So I'm gonna use Vim and I'm just gonna call my script my program. And the extension does not matter. I'm gonna do .sh. The extension is just there for you as a end user or developer that you know that's a shell script but the operating system does not care what the extension is. What it's gonna look at is the first line of your code. So let me go into here and this is not a Vim story, I'm not gonna teach you how to use Vim but again use whatever text editor you're comfortable with but you always wanna start your script off whatever scripting language it is for the most part with what's called the shebang line which is gonna be percent dollar sign and then a link to your executable. So in this case we're bash and this is very important because it's going to read that and then execute the rest of the commands based on whatever executable you give there. So if it's Python you're gonna wanna Python shebang, if it's Perl you're gonna wanna Perl shebang. If you don't put the shebang line it's gonna try to run it as whatever your default shell is which might be bash but may not be bash. So you wanna make sure that whatever you're writing this as you say bash, if it's a bash script you can also write scripts for a Z shell or fish or a shell so I guess you would call that ash. Anyway, that is very important you make that your first line. Now and there's different ways to write it but this is how we're writing it today. Now I can say echo and we just put in the commands that we've already done because a script is just a list of commands. Whatever you can type in the shell you can now put into a plain text file and basically the program's just gonna read that line by line just as if you typed it in the shell. There's just two things you need to make sure you need to make sure you have the shebang line and you also need to make sure that your script is set to executable. So we're gonna say echo hello world and then I'll say read dash p what is your name? And we'll put that as name and then we'll say echo and I'm just putting an extra space in the line there. Let's do it actually like this just to make it easier to read the code's gonna ignore blank lines. We'll say echo hello dollar sign name and then we'll do another read command dash p how old are you? And then we'll say age and then we'll say echo dollar sign name is dollar sign age years old. And then here's another command for you, the sleep command. The sleep command will basically just pause your script for a certain amount of time. If you just give it a number like three it's going to do it for three seconds. You can also do point three if you wanna have it pause for point three seconds. You can do three M if you wanted to wait three minutes or three H for three hours. You might be able to go D for days. I haven't tried that. Usually you're gonna be working in seconds or fractions of a second in a script. If you have your script pausing for hours or minutes or days there's probably a better way to go about that. Anyway, I just like I'll have it pause for we'll just say two seconds and then we'll say echo goodbye. Just to be polite. Now, we have a plain text file. This is no different than any other text file. We have a shebang line at the top which basically tells our system what interpreter to use because as far as the concern, this is a text file. One other thing you need to do is you need to change mod, whoops, plus X and the name of your script. You only need to do that once on your system but if you copy it to another system depending on how you copy it like if you were to download this from a website you might have to run that again but once you run it on your system once for that script you don't have to do it again and that's a security thing. That's to prevent you from programs from being downloaded and automatically running on your system or accidentally being run. Once it's made executable you just say dot slash in the name of your program as long as you're in the directory of your program. I'm gonna go hit enter. It's gonna say hello world was your name. I'll say Chris. It says hello Chris, how old are you? That's a typo obviously. I'll say 38 and it says Chris 38 years old. It waits two seconds and it says goodbye. Let's go back into our script and how old are you? We'll fix that and let's do some cleanup just to make our code a little bit nicer and this is up to you whether you do this or not. I commonly like to, depending on my script but for something like this clear the screen every once in a while so I'm gonna say clear the screen right away. So now we're gonna start off and our script so we don't see the rest of stuff that's already on the screen. Hello world, what is your name? Hello whoever. And then we can continue from there but what I'm actually gonna do here just to again personal preference. I'm gonna say sleep for two seconds, clear the screen again, run it again, sleep for two seconds, clear the screen. Depending on your code you may or may not want to be clearing the screen but for this particular program again I don't need to change mod again. I can just dot slash the name of the program and the reason you do the dot slash is it's saying dot slash is just saying the script in this current directory. As you can see I'm doing this tutorial in my temp directory just because it's not something I'm gonna save. I don't have to do dot slash. I can give the full path to my script like so and it's gonna run the same way. Hello world, what is your name? I'll say John this time. Hello John, wait two seconds, clear the screen. How old are you? I'll say 40 in this case. John is 40 years old. Wait two seconds, clear the screen and say goodbye. So dot slash just means it's a script in this directory because if you don't have the dot slash what it's going to do is it's going to look at what's in your executable directories, your path. So it's like, I'll go into this in more depth in another video but it's going to look in folders such as your bin folder or your USR bin folder. There's certain directories where you can put code so if you want this program to be executable anywhere on your system you can put it in one of those directories, those paths. Anyway, so yeah, that's your first script. It's very, very basic but you've learned how to so far print stuff to the screen, get user input and store that. That is going to be a lot of what you do in programming and next video we'll look at saving stuff to a file and retrieving it from the file and then we'll start using that for more practical applications. So thank you for watching. Visit filmsbychris.com. That's Chris the K. There's a link in description. As always, I hope that you have a great day.