 Hello and welcome. Today, we are going to be learning the Linux shell, which isn't a very clear term. And so I just want to start off talking about that. What is the Linux shell? Really, Linux has nothing to do with the shell. Linux is a kernel for an operating system. What we're going to be looking at today is what's called Bash. There are multiple different shells out there, and Bash is one of the most common ones. And so for example, on my system, I run ZShell or ZSH as my default shell. But Bash is around and used in a lot of systems. Used to be the default on the Mac operating systems and is default on a lot of Linux distros. I think that the Mac OS has switched to ZShell as a default, but I'm assuming that Bash is still installed on Apple devices, Apple desktops and laptops by default. Someone, if anyone has one of those devices, you can confirm with me. But since Bash is so commonly used in scripts, I'm assuming even though the default shell on Apple computers is ZShell, that Bash is still around. Now, I once had a viewer say to me, oh, Macs have Linux built in. And I thought he misspoke and he meant like current versions of Windows, newer versions of Windows have the Windows subsystem for Linux. And then talking to him a little bit more, I realized, no, he thought that the shell, the terminal, which are two different things, was Linux. And Linux is something, Linux is a kernel. It's a piece of software that basically allows the software on your computer to communicate with the hardware. So you never ever interact with it. The shells we use are shells. They're just programs and they run on multiple operating systems. So again, Bash is what we're looking at today. You can run Bash natively on Windows, at least from XP on up. I don't know about before that. Like I said, it's probably installed default on Apple devices and on most Linux distributions. And lighter weight systems like routers and modems and TVs and smart plugs and all your smart devices that run Linux usually have a shell. They may not have Bash installed. What they will normally have is a program called Busybox or possibly Toybox. I've talked about Busybox a lot in the past. It is a wonderful tool. Toybox is someone trying to recreate Busybox on our different license. It isn't as full. A lot of Android devices will have Toybox installed instead of Busybox. It just depends on your device. But Busybox is a single binary, a single program that's usually around a meg to a megabyte and a half. And it's not just the shell, but a whole bunch of your core tools that you normally use, all built into this one binary file. And so you will find it everywhere. And if you don't have it, it's easily installed on almost any device. And we'll look at that a little bit in the future. But right now I'm going to go over how to use Bash. And there's so much that you can do. I'm going to start with some of the basic things you do anytime you learn a new programming language. We're going to learn how to get user input, display information to the screen, how to save that information to a file and how to retrieve that. That's what we're looking at today. We're going to put that in a script. But let's go ahead and jump into our shell here and have a quick look. Okay, so here we are. First of all, we're going to learn about the echo command. So this is how the echo command works. You just type in echo and you can type in something like hello world, and it will just print out the text that I just typed. See hello world. So you could do it just like that, but it's a good habit to put your strings, which are your letters and words in quotation marks. So this will give us the same output, but you'll see as we go along that the quotations do make a difference. So we can say hello world. And we can also say hello world with single quotations, which is just an apostrophe on both sides. So what's the difference between these two? Well, there's special characters that you can use in your strings, in your words, that act differently. The single quotations here, which again is just an apostrophe, treat this more like a literal string. So for example, let's say I wanted to say hello world again, but let's say I wanted to say Tom said hello world. And I want to put this in quotation marks. We lose those quotation marks because it's seeing these quotation marks as special characters, which is supposed to be wrapping around our strings. So we have some options here. First of all, you have to spell world right. Now I'm going to hit up arrow. So up arrow and down arrow allows you to go through your previous commands. So you don't have to retype everything out. So I can say, okay, I want quotation marks. So what I'm going to do is I'm going to say back slash before these quotation marks. And what that's going to do is say, hey, treat these quotation marks as if they are quotation marks, not special characters. Boom. So another option would be to put it inside single quotes like this. That will also do it. But let's say I wanted to say Tom said it's a good day, right? We're going to have some problems here because it's seeing this single quote. It's thinking that we're ending here and that this is something different. It's looking for a closing quote, single quote, which doesn't happen. So if you're in a program like right now, it's still running. I'm not at my shell. Most programs you can kill or cancel with control C. So if you ever get stuck in a program at the shell, usually control and C will kill out of that. I'm also going to press control L, which is going to clear my screen. You can also type the clear command to clear the screen. We'll use that in a little bit. I'm going to hit up arrow again to go to my previous commands. And here what I can do, it might be an easiest option is to put this in quotation marks and then backslash out these quotation marks. And now we get Tom said it's a good day inside these quotations. So that's where it can get a little tricky is when you have quotation marks inside your quotations. So that's something to keep in mind. But another thing that you might have, I said echo hello world. And I can say like this and it will do fine. But if I say two exclamation marks, they're also called bangs. Okay, what just happened here? Well, let me again run this command, right? If I just at the shell here do bang bang or exclamation mark exclamation mark, what's saying is run the last command. So what's going to do is it's going to show me what the last command was, and then it's going to run it. So when we say this, it's saying, okay, echo hello world, run the last command and display the last command. So now we get all this. So again, there's special characters. And if I wanted to have those double exclamation marks here, it might be a good idea to use single quotes. So you have to keep that in mind. Another thing is let's say I want to say echo, I have five dollars. Okay, I have in the five dollars disappeared, right? Because in our shell here, dollar sign represents a variable. So it's looking for the variable five, which does not exist. So it's displaying nothing there. So let's go ahead and create a variable real quick. And all I have to do is let's say I want to create a variable called name. I'm going to say name equals and I'm going to say Chris. And then I can say echo my name is dollar sign name. And it replaces dollar sign name with whatever the variable equals. So if I was to change that, I can now say john. And if I was to run this command again, echo my name is dollar sign name, it's now going to say my name is john. Okay, so we've created variables. And we've shown how to use them. And we've shown some examples of special characters that might trip you up with the echo command. And again, if I wanted to say something like dollar sign name is dollar sign name. And I wanted to say this is dollar sign name and not what the variable equals. I'm going to say backslash. So this is saying, okay, this is not a special character, just print dollar sign and then the word name. Boom. So now we got name is john. So again, special characters can get a little confusing. That's why I'm bringing them in right off the bat because I might just show you the echo command. And also you go to put a dollar sign or a couple exclamation marks and things start acting weird or apostrophe. So I wanted to go over that right off the bat. So you can say the next character is a special character. Don't use it as a special character by using the backslash. Or you can use single quotes, but then single quotes might give you issues if you have apostrophes in your words. So we've learned how to save variables and print stuff to the screen. What about getting user input? So I'm going to use the read command. I can say read name, right? And now it's going to wait for me to enter something. So here I can say Bob, I just had the user input something and it saved it to the variable name. So now I can say echo you entered dollar sign name and it'll say you entered Bob. I can run the name command again and then I can say Tom. Now if I echo out you entered dollar sign name, it's going to say you entered dollar sign name. You just learned how to get user input, but we didn't prompt the user for anything. Now we can put multiple commands on one line by saying something like read name and then a semicolon means this is a new command. I can say echo you entered dollar sign name. And what it's going to do is I'm going to hit enter. It's going to wait because it's waiting for the user input information. I'll say Tim. And when I hit enter, it's going to continue with the next command and it'll say you entered Tim. So you can do commands on one line. So what a lot of people will do in a script and we'll create a script. And again, script is just basically everything we're typing at the shell here. We can put in a text file and bash will actually read that text file just like an actor would read a script and will act it all out. So I'm going to say echo what is your name and then I can say read name. So it's to say what is your name and I can say Sally and say that to a variable. And what I can do is now add another command. I can say hello dollar sign name. So again, it's going to, I'm going to hit enter. It runs this first command waits for me to enter something. I'll say Tim. And when I hit enter, it's going to say hello Tim. So I'm running multiple commands on one line, but we don't need to do this. So the read me commands, some of the commands we run are external programs and some are built into the bash shell. In this particular case, the read command is built into bash. And this is an example of where one shell may differ from another because I can while in bash say read dash P. So I'm giving it the P option, which is for prompt. I can say enter name and I can say name and then I can say new command echo hello dollar sign name. And this doesn't have to be in quotations, but again, it's a good practice. I'm going to hit enter. It's going to say enter name. I'll say Chris and it'll say hello Chris. Now if I was to come down here, this is a Z shell prompt. So I can know I type in echo dollar sign zero. That is one way dollar sign zero is a special variable, which I'll talk about in later videos, but it's telling me right here that we're in the Z shell. If I was to come up here and say echo dollar sign zero, it's going to say bash because this is my bash prompt. Okay. And if I say bash shell, someone's going to get mad at me because SH stands for shell. So bash BA stands for born again. So this is the born again shell. So if I say bash shell, that means that's like saying ATM machine means ATM automatic telling machine machine. Anyway, I just wanted to clarify that because someone will get on my case for saying bash shell. So down here, if I was to say read, and I was to say dash P, enter your name, name, saying that variable, and then just trying to do this is going to give me an error because again, the read command in this case is built into our shell and it handles it different. And in the Z shell, their read command does not have a dash P option. It may have some other option on getting prompts, but it doesn't have that dash P option. So just wanted to clarify that that that is one of the differences. A lot of shells are very similar in a lot of ways, but different when we're talking about internal commands. So now that we have all that, what do I want to do next? Let's go back to the echo command a little bit. I want to talk about a few other things. So I can say echo hello world. And what I can do is I can put in a character here backslash n, okay? And then I can say it's a nice day, right? And what it's going to print is print hello world backslash and it's a nice day. Well, the backslash n is what is a special character that we can say, look at certain special characters. This is a simplification of what this means, but I can say dash n. And so now when I do this, not dash n, sorry, dash n means no new line. We'll go over that in a moment. Dash E. And what that did now is it looks at this backslash n as a new line character. So now it printed hello world. And then it went to a new line and said, it's a nice day. If I was to put two backslashes backslash ends, I mean, I'll say hello world, enter, enter, basically new line, new line. It's a nice day. Another option is backslash t, which is a tab character. So it's like pressing tab on the keyboard. And again, both those only work when you have that dash E option. Otherwise it's going to print that literally as backslash t. And you can combine these so I can say backslash n, backslash t, and have to have that dash E option. And it says hello world, new line, tab over, it's a nice day. And then I can do that again here, backslash n, backslash t, and I can say the sun is out. And so now we got hello world, new line, indented in, it's a nice day. The sun is out. So I just wanted to show you that. So as of right now, we have learned how to print stuff to the screen, get user information, save variable, do we go over save variables? We did go over save variables, save user input as a variable. And then, you know, print that out to the screen. Let's talk about saving stuff to a file. So a command you'll use a lot at the shell will be LS, which is to list files. I'm in a current directory that I created for the tutorial. There's nothing in it. I typed LS, nothing exists, right? So what I can do is I can say echo, Chris, right? And I'll echo Chris. But if I say the greater than symbol and give it a file name, I'll just say it name dot LST, which is just a text file. I'm just giving it the extension of LST for list. I do that and we don't get Chris to the shell. But if I list out my files now, you'll see that there is a file called list or name dot list. I can now use the cat command on any file to see what's in that file. Cat is short for concatenate, which is actually used to combine files together. But you can use it to display what's inside a file, cat in the name of the file. I'll hit enter and it shows me what's in that file. I can run this command again and I can change it to John. And now the word John is in this file. So I can cat name dot LS and we get John. Because I created, I overwrote that file by say greater than. If instead of saying greater than, I say greater than greater than, that means append to this file. So I can say Chris. Now, if I caught up cat out that file, I'm saying John and Chris. Now also, there's tab completion on most shells. So you notice that I can start typing cat and I can type in N. And if I hit tab, it auto completes what I'm typing. It guesses, it goes, okay, you want a file, let's look at the file names. There's only one file that starts with N. Let's put that. And so you don't have to type out full things. I can also do CA and hit tab and hit tab twice. It's going to give me a list of commands and then I can hit T or whatever and different shells will complete that different ways depending on how you have it set up. But the default is to just start listing stuff and then I can go T and now I hit control L again to clear the screen and I can now hit tab. I don't have to type anything because there's only one file in this directory. Now if I come back up here, I can add Chris to that file again and now it says John, Chris, Chris. And if I want to, I can create another file and I can call this list dot list. Sure. Now, sorry, not cat. I meant to do into list dot list. So it's telling me that there's no such file or directory called list dot LS or LST. Now if I list out, you can see I have two files. If I type in cat space and hit enter or tab, you can see, oh, these are my options. Now I can hit L and tab and it will auto complete. And again, I can add more names to that. Let's start creating a script really quick. I'm almost done with this part, this tutorial. I'll start another one. But we are going to use a text editor. The text editor I like is called Vim, but you can use whatever text there you like, but I'm going to use Vim. Actually, technically I'm using what's called Neo Vim in most cases, but this is usually what I use by default. Today I'm using Vim and I'm just going to call it my script dot SH. The dot SH is a file extension that means absolutely nothing to the shell. It's just going to see this as a text file. Once we're inside our file here, what I need to do is type in bin bash. Now, this isn't a Vim tutorial, so I'm not going to go over too much on Vim. You can use whatever text editor you like, but let me quit out of this, but I will go over some basic stuff. So I'm saying Vim, if you have Vim installed, it's a text editor. Come in here to start typing, I have to type in I for insert mode. Okay. Now I'm giving what it's called was called the shebang line. The first line of any script you write tells it what interpreter to use. Again, we're using the bash shell. It's saying use the bash program to read every line in this file. If you're using Python, you would have to point it to your Python executable. If you're using Perl, you have to point it to your Perl executable. So the shebang line is very important. Okay. So now I can just say echo hello world. I can save this. So I hit escape to get out of insert mode. Now I'm going to hit shift and I'm going to put a colon here, WQ. Again, I'm not going over Vim much in this story. I just want to say that in case you try to use it, use whatever text editor you like. It could be a graphical one. I mean, you could write these in notepad if you wanted. If you were on a Windows machine, I don't highly suggest it. Anyway, so I just created a file. Again, I can cat out what's in that file. That's what my script looks like on a Unix and Unix like operating system. Linux is a Unix like operating system. You have to make executable programs executable. You can't just run it without saying this is a program basically. So I'm going to say change mod, change the mode plus X. It means make it executable and give it the file name. Now I can say dot slash in the name of the file. Why dot slash? Dot slash just means it's in the current directory. If I wasn't in this directory, I could tell it where it was. It's in the temp b directory. But since it's in this directory, we're going to say dot slash. Why do you do that instead of just this on other operating systems like Windows? When I say other operating systems, I think just Windows. When you run a command, it's going to look at your current directory for most things by default. Now imagine you're running different commands and let's say I have a command on my system called nmap. If we didn't do dot slash, if I went to a directory and I didn't know there was a program in there called nmap and I try to run that, it might try to run that script first and it could be malicious and I would not even know. So it's a security thing that if something's not put in your path directory, which we will get into more in future tutorials, you have to say specifically I want to run the one in this directory. So dot slash just means this current directory run this script. Enter. And again, we have to make it executable just the one time on a new system. If you were to download this from a website, you'd have to do it one time just to make sure you don't download something and it runs without your knowledge because you accidentally clicked on it or accidentally typed in it thinking you're running a different command. Okay, so let's go back into our script here. So again, I can put another line. I can say hello, Chris. I can run it again, again dot slash and the name of the script. Let's go ahead and create a variable. I'm just going to say name equals and you can put spaces in a bash script for your rails. So if I did this, that would not work. You can see that in Vim, if you have color coding, color highlighting, highlighting setup, it's going to tell you that something's wrong because you can see the colors change. So I'm just going to say Chris for this and I can just say dollar sign name. Now if we run our script, it's going to say hello world, hello, Chris. But again, what we want to do is we can say welcome and I'm going to say read dash p and it's okay that we're doing this because we're telling it it's a bash script. So we know that it's using bash in this case. Enter your name, name. I'm going to run this script and I'm going to say Tom and now it'll say hello Tom. If I was to leave it empty, it's going to say hello nothing. In a future video, we'll talk about how to check making sure that that variable exists. Anyway, I'm going to stop here on this video and I will continue this tutorial in my next video. I do thank you for watching. Please visit filmsbychrist.com. That's Chris the K. There's a link in the description. I thank you for watching. I hope you found this useful. We're going to go in a lot more in depth in future videos. So keep on watching. Subscribe so you don't miss anything. Check out my Patreon page, PayPal page, Laidbury page, Libre pay page. I should say buy me a coffee. All that stuff you go to filmsbychrist.com. There's either links to that stuff in the description of this video or filmsbychrist.com. You can click on the support section and support me in any of those ways. I do thank you for watching. As always, I hope that you have a great day.