 Hello, and welcome. I have hundreds, if not thousands, of shell script video tutorials on my YouTube channel. But I try to, every once in a while, go back to the basics for those beginners. So today I'm just going to sit down and I'm going to start writing a script. I'm going to kind of make it up as I go along, but show some basic concepts of shell scripts and how they work. We're going to focus on bash in the tutorial because that's very commonly the default shell on many, many systems. This is what we're going to go over today. We'll span out to other shell interpreters, but I'm going to focus on bash today. So let's go ahead and again, I'm just going to wing this and see how it goes. Here we are on our shell. First of all, as I said, we're going to be working with bash today. Normally, my default shell on my system for when I go to my terminal, my shell, I use these shell, but scripts I usually write in bash since that's one of the most common defaults on systems. Now to confirm what your shell is, one thing you can do is you type in echo $0 at your shell and it should tell you what your shell is. In this case, I'm using bash. So anything that starts with $ in a shell script is going to be a variable. And then zero just means show me the variable for basically what is the main executable that we're running in this case since we're at the shell prompt that are prompt here. We'll tell us what shell we're in. Let's talk about the echo command. The echo command will echo whatever you say to it. So I can say hello world and it printed out hello world. And again, we can create variables just by saying something like, let's say, I want to say my name is Chris, right? And now I can say echo $name and it'll say Chris or I can say echo $name and it says Chris. And if I change that variable to something like John, if I run that command again, it will now instead of saying hello Chris, it'll say hello John. So there we go now, a way you can get to user input. So let's say you want to ask them a question and get the input. You would use the read command. So I can say read and then I can give it a variable. So I can say like name again. And when I do that, it's just going to pause here and I can type in something whenever I type is going to go into the variable name. So I can change it back and now I'll just say Bob, right? Bob and now if I say hello name, it says hello Bob. But just saying read like that will not tell the user what to do. So in a script, you can use the echo command then the read command. But in bash, you don't need to do that. What I can do is I can do dash p and then in quotation, say something like, please enter your name. And now it's going to say, please enter your name. And I can go back to saying Chris. And if I say echo, hey dollar sign name, it will say, hey Chris. Now the read command, some commands we're going to do are built into your shell and some are external commands. The echo command is built into bash and the read command is built into bash or whatever shell you're running. So they may vary slightly different shells. So example, if I open up another terminal here, so down here at the bottom of the screen, this is actually the Z shell. If I try to run that same command, so I can do read and name and I can type in Chris and I can say echo dollar sign name and it will print that out. But if I try to use that prompt command dash p and I can say please, I'm just going to say name, I'm not going to type that all out because it's going to give me an error. Because the read command in the Z shell doesn't have that dash p property. It might have something similar. I'm not familiar with it. If you go into its manual file, you can find out. So what is the manual file? When you're in a shell, pretty much every shell command you have should have a readme or a manual file that tells you how it works. Some manual files are better than others. But if I was to do man and I was typing read here, it's going to give me basically a big readme file that I can read through and find out all about the readme command or read, not readme command, but read command. So and you can even do something like man bash and it's going to give you a whole bunch of stuff about bash, right? It's going to be a long one because bash is a pretty complex program. So if you're never ever not sure what to do something, Googling it is helpful, but also using the man files helpful. And a good man file will usually have examples at the bottom. That's not always true, but it's nice when they do. So far we've been running commands, you know, creating variables and echoing stuff out and getting user input right here at the shell, but that's not how you're going. You'll use the shell up. If you're going to write a script, you're going to write a script, which means you need to create basically a text file. Now you can use whatever text file you want. Oh, also there's a clear command that clears the screen. But also if you're at the shell, so let me just run a couple of commands here just to put something on screen. You can also hit control L in most cases to clear the screen so you don't have to type clear. When you're creating a script, you would have to type clear to clear the screen. So we're going to create a script now, right? That's the point of this video. You can use whatever text editor you like. Some people like Nano. You can use graphical ones like G-Edit. I think there's a K-Edit as well. There's lots of them. I think one's called Leafpad. I prefer Vim, which some people consider complex. But you can use whatever text editor you want. It doesn't matter as long as it's a text editor and not like a word document editor. So don't use like Microsoft Word or, you know, open up, open office and try to type something. And I suppose you could and then save it as a text file, but we're not going to get into that. I'm just going to create a file called my script, right? Now, normally if the shell script file, you're going to do .sh, but the extension does not matter. Your computer does not care about the extension. That's there for you. What matters is the first line of the script, which is called the shebang line, which in this case, since it's a bash script we're running, we would do pound, exclamation mark, forward slash bin, forward slash bash. Because what that is, your bash shell, your bash interpreter or your Z shell interpreter or whatever shell you use, that's a program that's basically going to read this file line by line, but you have to tell it which one to use because it could be a Python script. Some people write scripts in Python and you would tell it where Python is on the system and what version of Python and it would start reading this file. So the shebang line is important because if you don't put that, what will normally happen is it's going to try to run the script with whatever the default shell is and it may not be bash and you might have some issues. So let's go ahead and now you just, right here, you write commands like you would at the shell, but you're saving us a text file and again it's going to read through it line by line so I can say echo hello world, right? I'm going to save that file and now the first time you run it, you need to make it executable so if I do .slash my script .sh it's going to tell me that it doesn't have permission to run. This is a security thing. You don't want executables, you want programs that you download to be able to just run by accident. So you have to make them executable and that's super simple. You just say change mod plus x for executable and then you're going to save the name of the file. You don't have to do that again on this system, as long as you do it once on this system, it will always be executable. The only time it won't be is like if you download it from a website you will have to make it executable again. So what's the .slash part of that? The .slash is saying look for the script in the current directory, otherwise it's going to look for it in a system directory, your system path, one of your path files, which we'll talk about in another video, but .slash just says run the script in this folder because you might have a my script .sh somewhere else on your system. It's saying look in this directory. So run it from this directory, we'll hit enter and there it says hello world. Also at your shell, you should be able to hit your up and down arrow if you haven't noticed me doing that yet to go through previous commands. There's a lot of shortcuts in the actual shell that we can look at. I'm not going to get into them too much. I'm going to go back up to my vim command, again, whatever text editor you use. And in here, I'm going to add in that readme command or read, not readme, I keep saying readme, read command. I'm going to say prompt, please enter your name and that'll give it the variable name. And then down here, I can just say, I just realized I spelled world wrong, name. Okay, and now we've changed that again. We don't have to make it executable again. We'll just run that script and I'll say please enter your name. I'll say John, I'll say hello John. Now another command we can look at here is a sleep command. You give it a number and it will sleep for that number a second. So I can say one second. It just pauses for one second. If I say two, it's going to go two seconds. I can also say like one M, which would be one minute. Now if a command is running and you want to kill it or we'll say cancel it, it's control C. Control C will cancel that command in most cases. You can also do fractions of a second. So if I want to say a half a second, I can say sleep 0.5 and it will sleep 0.5 seconds. There's other options there. Again, you can use the man command for manual and look at other options for the sleep command. Actually, there's not much in there. So forget that. Usually there's more options. This is a very basic command. So let's do this. Let's go back into our script and what I'm actually going to do here is I'm going to add a clear command at the top and then down here, I'm going to add in a sleep for two seconds. So now when I run my script, it's going to clear the screen, ask my name. I'm going to say Bob this time. We'll say, hello, Bob. It will wait one or two seconds. I forget what I put in there and then exit out. So it kind of holds it there. So now what we can do is we can add in another line and I can say, let's clear the screen again. Just for fun, we're going to clear the screen. Depending on what you do and lots of you don't want to clear the screen because you want to leave information on the screen but this little program is just kind of interacting with the user. So clearing the screen might be nice. I can say, hey, dollar sign name. What is your favorite color? And here we can put color. And then when they hit enter, I can say echo. I like dollar sign color as well. And then we'll say sleep for two seconds. And then we'll say echo. Bye. We'll run our command again. And this time I'll put it in my name, Chris. Hello, Chris. And then it says, hey, Chris, what is your favorite color? And I'll say red. And it says, I like red as well. Bye. Now, let's have a little more fun with this. We can again create variables at the top or how about this, we'll run the date command. If you just run the date command, it tells you the date and time. But you can also format different ways. I can say plus percent F, capital F, and it's gonna format it like this with the year, month, and day. You can do other things as well. I can just do capital Y and it will give me the year. And what I can do is I can do dash and then percent. I think lowercase M is the month and I can do dash, percent, D for day and it will give me the day. And of course I can put that in different orders. If I wanted to go year at the end and days at the beginning, I can do that. I can do lowercase Y, I think, will give me just the two digit for the year. And if I do, I can also put other things in between these. I can go like this. I can do slashes instead of dashes. Again, if you look at the man file, this man file will have a lot of information in it. You can see a lot of these different, I'm getting a spam call because I forgot to turn off my phone. Let me take care of that real quick. I'm usually better about turning off my phone before videos, power off. So that was a spam call. Go ahead and feel free to call that number and tell them not to call me anymore. You can see all these different options here. So if you wanted the full day, a weekday name, like Sunday, you can do capital A. You can do a abbreviation of the month with lowercase B. The full name of the month would be. So let's go ahead and give those a couple of tries. So I can go here and I would say lowercase B is the month. Capital B is the full month. What do we say? A is the day of the week. And I guess capital A would be the full name. So you can do all these different things and use them in your scripts. Let's go ahead and use them in our scripts. I hit control L again to clear my screen. And I am gonna come in here and I'm gonna be like, let's see, I can use that command. Again, I can just put the date command in here. And if I run my script like so, I can say John. John, I can say blue. And then it's gonna print the date there. And it's gonna say bye. But I could do this. I could put that into a variable. And I could say something like D equals. And then if I put inside, I'm gonna put inside quotations because I want this to be a string. But dollar sign and then parentheses, I can put a command. And whatever that output of that command is will now be in the variable D. So I can do plus percent F. And then down here I can say echo did you know the date and time is dollar sign D? Question mark. Now, if I run this, again, I'll say John and I'll say blue. And then it says, oh, I didn't, did you know the date and time is, that's the date? Did I do plus F? What did I do? Yeah, so if I just do it without the plus F and I just do the date, it will give the date and time. So again, John and we'll say green. And it says, did you know that that date and time is this? But I'm just trying to give you examples of how you can use commands in your output. Now, I don't have to put that into a variable, right? I could come down here and I can just say date. And it will do the same thing. So I'll say Chris, I'll say red. And you can see it output the same command. So basically, anytime you put dollar sign and then a command inside parentheses, and it can be multiple commands, the output of that will be placed there as a string. So you can put it into a variable or you can put it into here. Why would you do one or the other? Well, if this command was really long, you probably wouldn't want it in your echo command. You could break it up, put it in a variable, then put it there. But also, let's say you want to get the current time at a certain point in your script and use that at certain points in the script but you don't want it to change. If you ran the date command each time, it's going to change that time each time you call that command. A plus commands could take a while to run. So you may wanna run it once, put it in a variable and then use it multiple times in your script. Let's see, what else can we go over today? Oh, here's an important one. Writing to files and reading from files. So let's go back out to our shell here and let's say I say echo and I can say just my name, Chris, right? It echoes it out. But if I use a greater than symbol and then give a file name like name.txt, now I just create a file called name.txt and within it, if I type in, you know, use my text editor, I can open it up and it says Chris. If I was to run that command again with another name, what it's gonna do is going to overwrite that. Basically it just took that file, it raced the old one and created a new one, right? Says John inside. If I wanted to add to it, instead of just doing greater than, I'm gonna do greater than greater than and that will append it to it. So instead of John here, I'll say Bob and if I open up that file, it now says John Bob and if I was to run that again with maybe Sally. I can open up that file and now it says John Bob Sally, right? Let's say I don't wanna open up my text editor every time I wanna see what's in that file. I can use the cat command, cat in the name of the file and it will cat out, it will display on the screen what you do, why cat, that's short for concatenate. It's actually a command that's designed to allow you to attach multiple files together, concatenate them together. But most of the time it's used just to display what's inside a file. So we've written to a file, created a new file, we've appended to a file, we've read the file but things you can also do the same thing with the output of any command. So the output of this echo command is Sally I put it in there, but if I was to do date, right? Date gives us the date and time. I can append that to the file as well. And now if I cat out name.txt, it has here the output of that command. So the output of any command can be dumped into a file and then later read. Again, if I was to come in here and change this back to a single grader then, it will then put that into the file overriding everything else. And now we just have the date, so you can put a timestamp in the file if you want, right? And of course, let's say we wanted to create a log. We want the date and time of something that happens and then what that is that happens. So clear the screen here real quick. I can again come up here and I can say echo and I'm going to do this and I'm going to do pipe symbol and then I'm going to say John entered room, right? So I'm going to do that greater than, I'm just going to call this access.log. Now if I cat out access.log gives me a date and time and it tells me that John entered room. So let's say you have sensors in a room and you want to have the script run whenever someone enters the room they scan their key card or whatever. Well, now we know when John entered the room and then later on we come in, you know, Sally comes and scans her ID badge. And now if we cat that out, we can see, okay, at this date and this time on this year, this time zone, Sally entered the room. So we can do stuff like that. So again, dollar sign and parentheses will run a command and wherever the output of that command will be put in the place of this as a string. A string is just text, right? So we're echoing out the output of that command and whatever text we want here. And I divided it up by a pipe symbol. Pipe symbol can be used to pipe different commands into another command, the output of one command into another. I'm not sure if I want to get into that into this video too much. That's something that you will learn is one of the things that makes Shell Scripts super powerful is that you can take the output of one command and put it into another. Kind of like we've dumped stuff into a file, but instead of doing that, we want to put it into another command, which actually maybe I'll do in the next video. Maybe I'll have a second part of this. But I just wanted to go over this real quick. I hope you learned a lot. So we learned how to display text on the screen, get user input into a variable, how to do stuff with that variable. Let's do a little bit more with that. Let's go into it just as a little bit more of a overview of what we're doing. I'm gonna say I'm gonna delete this and this and then I'm going to say clear the screen again, read-p, what is your friend's name? And we'll say name, or we'll just say friend, right? And then I'm going to, I'll clear the screen again and I'll say echo. One day dollar sign name tried Linux. Dollar sign name loved Linux. Dollar sign name went and told dollar sign friend about Linux. And then we'll say dollar sign friend didn't get it at first comma. But after trying, trying Linux, testing it out, dollar sign friend saw what dollar sign name was talking about. And they both love dollar sign color, right? So I have multi-lines. I echoed out, I only have the quotations at the beginning and the end here so I can do multiple lines. If I put a quotation mark somewhere inside here, I would have to backslash it out. So if there was quotation, let's say went and told was part of quotation, I couldn't do this because it would mess up our echo command. So I would have to put a backslash before that to indicate that these are actually supposed to be quotations, but that's not an issue right now with what we're doing. And I wanted to say that in case you threw some in there. So if I did everything right, I can say my script. I can say my name is Chris. I can say I like plaque and my friend's name is John. And it says one day, Chris tried Linux, Chris loved Linux. Chris went and told John about Linux. John didn't get it at first, but after testing it out, John saw what Chris was talking about and they both love black. Bye. That's our script. So I do hope you learned something and I hope that you enjoyed this and I hope that you start writing scripts. I mean, a lot of script writing are the commands and things that we went over today. Beyond that is basically if then statements, testing stuff and then basically sorting and displaying data in different ways. So you're 80% there to writing most scripts, I would say. So thanks again for watching. Phil's by chris.com. Go ahead, check out my website and I hope that you have a great day.