Day 8 - Bash | Writing our first script
By Drew Karriker
June 1, 2022
0:00 / 7:17
Drew K00:00

Hey, there. Welcome to coding with Drew in this section, we're going to write our first bash script. I'm really excited to teach you this second. We've gone over a whole bunch of basic commands, for our terminal. You could call them bash commands, but they're more terminal commands and these are pretty standard commands that you've learned.

And now we can start scripting so we can start chaining those together to make a desired outcome. So let's go ahead and get started. You'll see that, in the CD you can CD into. Tilda, dev slash bash. That's where I'm at right now. And we're going to touch and create a new file.

So touch just the command to create a new file for you remember, we're going to create hello, underscore world dot S H. Alright.

So if we LS, we can see that file was. Now let's use our nano editor. Let's continue on using nano editor for now. We'll switch back over to visual studio code, later, but just for your first script, we're going to use nano just to continue press.

So hello world S H and we're going to open it up. So here's our blank canvas to start writing bash and Subash follows the same format for every single file that you see. And the first line on every bash script, we'll always start with the shebang line. Shebang is hashtag and then a bang or exclamation.

I don't know why it's called a shebang, but that's what that's, what's called. Now that I'm saying it out loud, it sounds odd, but here we go. So shebang, and then you specify where to locate, your executable file for that script, the interpreter. And so just to give you an idea, this is actually located inside of your root directory.

In a directory called bin, and hear, you can see bash. There's also ZSH there's cat, which we've used their ch mod and these all kind of come standard on a Mac. They're all built in already. Pre-made shell interpreter. Okay, so we're going to use slash bin slash bash, but if you wanted to, you could use Esh, but this is a bash course so let's stick with bash.

Okay. So that's the first thing. Now, everything else is optional, but I recommend it. So we're gonna have. Hashtag now hashtag in bash means a comment. So just like when you press a command slash in, in visual studio code and produce a comment. It will be a comment. It doesn't work in nano.

So you have to hashtag, and the first thing we're going to write is more specifically, like what is this command? What is it. So we're going to say author and I can put my name and then we're going to say hashtag date created. And we're going to say, today is 5, 12 22, and let's say last modified 5, 12, 20.

And then we're going to say hashtag space description, and we're going to give it a description. We're going to say this is our first shell script. It should print. Hello world. Okay. So then we're going to type in hashtag usage is how you would say, how do I call this command? How do I utilize this command?

So we're going to specify that you just run the file. Hello world. The sh. Now, remember when I said that? The dot slash doesn't work the way you think it does with bash. And this would have worked with HTML, but not with bash. This would in HTML would mean this directory slash and then the file in bash.

This means load this file and. It would be based off of the current directory or relative to the current directory. Okay. So like I said, these things are optional, but they make your scripts look a lot more professional and easier to follow along. If say you're coming to somebody, else's code somebody else's coming to your code and they're looking at it for the first time, they would have a better understanding of what it does.

So we learned about echo and so echo. Is the next command that we're going to put in here and echo will repeat into standard out whatever we put fall, followed by the echo inside of our quote marks. Let's put an exclamation mark in there.

You saw me try to use my mouse. It's common to actually end up making that mistake, but, all right, so now we have our hello world. The next thing that's required after our script is an exit command. So, if this command was to complete, successfully itch, Exit zero and any other exit number would be an error.

Okay. So just to recap, always has to have she bang line. Then you have your code and then an exit. This block here is extra. This is recommended. This will make you look more professional when somebody else is reviewing your code, and if you post your scripts somewhere for other people to use, it's a lot easier to understand what the purpose of the script is from reading this than it is read a whole bunch of bash script.

That's been minified and hard to understand at a glance.

Okay. So let's save this let's control X and then type Y. Alright. So we have this file in here and if I wanted to run it right now, it won't allow me to. So dot slash remember, that's how you call a command to execute. Say hello world.

S H. Now if I run this, it says permission denied. I wonder why. This is where we're going to step into our permissions topic. I'll see you in the next video where we learn all about permissions, but before we do, I recommend that you take solid notes on what you've already learned using nano to create your own first hello world, shell script.

And after you're done with that, I'll see you in the next video.