BASH scripting will change your life

Video Notes:

Sign up for Linode here: and you get a $100 Credit good for 60 days as a new user!

Continue on your journey to becoming a better nerd with NetworkChuck as he shows you how to start your day off right using a BASH script!! Did you know that a BASH script can change your life? In this episode of Bash scripting RIGHT NOW, we cover a life changing bash script that uses the new concepts of VARIABLES and ARGUMENTS.

🔥🔥Get access to the EXTENDED version with extra BASH goodness: (Join NetworkChuck ACADEMY!!)

**Sponsored by Linode

0:00 ⏩ Intro
0:51 ⏩ Quick challenge!
1:25 ⏩ Getting your Linux lab ready!
2:33 ⏩ Challenge review!
3:59 ⏩ What the junk is a variable?
6:32 ⏩ Making bash read
8:16 ⏩ Arguments, the not yelling kind.
9:03 ⏩ Make your own argument!
11:12 ⏩ Making even more variables!
11:53 ⏩ Using our new variables!
13:11 ⏩ Outro

We already know that bash is powerful and it will make you an awesome it engineer, but it can also change your life. Well, kind of it can actually make each day of your life better. So in this video, allow me to introduce the bash Butler, a script designed to make you feel awesome and informed every day. In fact, here’s a sneak peek. Whoa, whoa, hold on, buddy. You gotta watch the video first because there are things in there that might be new to you. Like variables and arguments, really fun stuff that you’re gonna learn in this video. So my bash friends, my bash club, my bash brothers and sisters, that’s probably better hit your coffee. Ready? Let’s dive deeper into bash. The first thing we’re gonna do is actually start writing a script with the knowledge we gained in the first episode and see if you can do this by yourself. Here’s the criteria, name, your script best day And I want your script to greet you with these three messages and feel free to replace Chuck with your name and bonus. Give one set of waiting time between these two messages and finally make your script executable. Pause the video. Go
UN pause. Let’s walk through it real quick. And by the way, if you have no idea how to do this, that’s fine. Just go back and watch episode one. I show you how to do this and you’ll need that knowledge in order for us to continue building this crazy awesome script. Just trust me. Here we go. Now first I’ll spin up my Linux lab by going to in the Chuck here in ode, I’ll actually create a virtual machine, a Linux virtual machine in the cloud, and it will only cost me one penny per hour, which is insane. And if you’re neutral ode, you get a hundred dollars free credit for 60 days just for signing up. And as you can probably see, ode is my cloud provider of choice. I have like a million virtual machines in the cloud. It’s how I run my business.
And it’s my favorite play used to practice Lennox because I can quickly spin up a Lenox lab in the cloud. It boots up in seconds and when I’m done, I’ll delete it. So it literally only costs me one penny. So if you wanna learn Lennox the best way you gotta get hands on. So go and get logged in below. We, Chuck, and look, mine’s already up and running. So I’m gonna grab my SSH access over here just by copying this one little command and I’ll launch my command prompts in windows or terminal and Mac and Lennox, pace. My command hit enter, accept all fingerprints, put in my password and I’m it. And by the way, if you already have your own Linnux machine, that works too. So here we go. Time to write a script, quick coffee break. Don’t worry. It’ll be plenty of these. And I will go a bit fast. If you are confused, go back to epic. Oh one, here we go. First, let’s create a script named best day let’s do it nano best day ever. DOH done. I’m already editing a script. And how do we start each script Shang? Sorry. Didn’t mean to scare you shebang in place, Rockton task. Number two, let’s knock it out. I’m gonna use the echo command to talk to myself or have my script. Talk to me and I’ll use a sleep command to make it sleep in between commands
Looks good, task two, done task. Number three, let’s make the sucker executable control X Y enter to save. And then I’ll do C H mod plus X and the name of my script. Bam. We did it. If you did that by yourself. That’s awesome. And what do you say? We actually run our script,
Man. I already feel so great. Best way to start the day. Don’t you feel awesome, but there’s more. We can do more, but hold on. Before we do that, this has awesome and really great for us. You Chuck, whoever you are, but what if our friend Patricia, what if she also wants to start her day? Awesome. And honestly, she needs this cuz she can’t even get into a coffee shop. So let me ask you, what do we have to do to make sure that this says Patricia so that she has a great day? Well, you might think let’s just go edit the script and we could do that. We’ll type in nano once more, the name of our script, best day ever dots sh and edit that sucker. And we could painstakingly go through and change each of these names, but we can’t do that.
We’re it people and we are lazy. So I wish there was a way we could just change all the names at once. There is. So that’s why we’re doing this right there is it’s really cool. Check this up new concept time. We’re going to create something called a variable. Go ahead for me and type in name. That’s it. And then do the equal sign. And then in quotations type in let’s just say Patricia and then close with quotes. There we go. This right here, this name, that sucker is a variable. And when you think of variable, I want you to think, Hey, it can change. And here’s what we’re gonna do with it. Ho ho instead of manually typing in Chuck or Patricia or Ben evil Ben, instead of doing that, we’ll go in here and I’ll use my Curia. Go ahead and do this with me right now.
I’m gonna take away Chuck and I’m gonna type in dollar sign name, and I’ll do it on every one of these lines here, remove all the chucks. Now we’ll walk through this, but I wanna just execute it real quick. Let’s do it together. So we’ll go ahead and exit and save our file. Control X Y enter and let’s to run our script period four slash best day ever. And check this out. How cool is that? It replaced the name that we put in there with a dollar sign with Patricia, let’s jump back in the script nano best day by simply typing in the word name and saying, Hey, I want this to equal something. And this example is just gonna be text that says, Patricia, we created a variable. And then we use that variable by using a dollar sign. And then the name of that variable, which it’s also name, which is kind of confusing. It’s okay. We we’ll get past it. So again, here we created the variable and here we used it. Let’s try it again real quick. Instead of Patricia, let’s change the variable name to Ben. Go ahead and do that for me real quick. Pause.
I’m pause coffee break. Now let’s do it. I’ll change Patricia to evil. Ben control X Y enter let’s run the script one more time, best day ever for evil. Ben, how cool is that? We only had to change one thing in our script and it changed everything else. That’s a power of variables and not only do they save us some time, that’s awesome. They are also cool, crazy powerful. And you’re about to see that. Now let’s jump back in our script nano best day ever do sh what we did here was cool. We manually set a variable name equals Eagle bin or Patricia, whoever it is, but that’s not the only way we can set variables. And that’s a good thing because at this point we still have to manually change something. And again, we’re lazy. We have to automate this. That’s what bass scripting’s all about.
So what I wanna do is change it to where whoever comes in and uses my best day ever script. It’ll just match to them. For example, if my wife Abby comes in, I wanted to just say Abby or my daughter, Chloe, or my brother Cameron, or my video editor, Nick. I want this script to make their best day ever happen. And I don’t want to have to do anything. How do we accomplish that two ways? Check this out first way. Go ahead and delete this right here. Take it out. Goodbye. Goodbye, April Ben. And this might feel a little weird, but go ahead and type this in read space name. Okay. What does that mean? I’ll tell you in a second before that, let’s go ahead and do an echo and say, what is your name? You probably see where this is going, but let’s, I’m not gonna spoil it for you. Let’s take control X Y enter to save and get outta there and let’s run our script best day ever at sh run it. What is your name? Okay. Maybe we should answer. Let’s say Nick,
What, how cool is that? We just got even lazier mission accomplished. Let’s get back into our script nano best day ever. Sh what just stinking happened instead of setting the variable name manually just saying name equals Patricia name equals Ben. We’re saying, Hey, why don’t we ask them what the variable should be? Let’s ask the user, the one using the script. And that’s what the read over here is doing. Read will get some user input and that input will be stored inside the name variable or the variable name. That’s amazing, right? That’s so fun. Think of all the things you could do with this. In fact, you should play around with this. Just have fun. Go crazy. Have your, your wife, your husband, your brother, your sister, your friend, your coworker, run the script and do something. I set a lot of names there. I’ve had a little bit of coffee this morning.
Now there is one more way to do this, and it’s really fun. Check us out. So go ahead and take out Rena. In fact, take echo. What is your name? Take out all that. And we’re gonna do this. And this is gonna feel weird too. Really weird type in name like we did before. Like when we we’re setting the variable manually type in equals, but this time do this with me. Dollar sign one. What, what is that? I’m not gonna tell you. We’re gonna run the script control X Y enter to save. I’m gonna do control. Will L to clear my screen, make it nice and clean, and we’re gonna run the script again, but hang with me real quick. Go and type in period slash best day ever. Sh but before we run it right after that, go ahead and put in a name.
I’ll see my wife, Abby. So Abby and go, what what’s happening? How did that get there? Right there? We just got into an, an argument, a really fun argument. That’s what these are call by the way. Let’s get back into our script, nano best ever. Ah, I’ve been saying that too many times. We just got into an argument with our script and we actually won and had fun specifically. This is called a positional argument or a positional parameter. And whenever we ran our script, you know, period, four slash best a ever SSH, whatever parameter we put right here after the script, we’ll match up $2, sign one keeping in mind that it’s the first parameter we’re putting. So it matches up to one first one, first one. Got it. Yes. Okay. And that means we can do this, check it out. We can have more parameters. For example, my wife, Abby probably doesn’t wanna be complimented on her beard. Just a wild guess. Probably won’t make her best day ever. So we gotta change that. So real quick, actually challenge knowing what we’ve done here, use a positional parameter and argument to make that change and make you unique to everyone who comes in. Pause a video
UN pause, coffee. Purple. So first I’m gonna set a new variable. Now I’m gonna use a positional parameter. It’s kind of fun. Say positional parameter. I wanna name mine compliments and I’ll have that equal dollar sign two. And then down here under beard, I’ll just put in the variable dollar sign complement. Bam. That should do it. Control X Y enter to save. Let’s try it out. So let’s run our script period. Four slash best day ever dot H I’ll say Abby as the name. And then my second parameter will go right after that. And I’ll say eyes, let’s try it out. This is so cool. Right? Bam. Good morning, Abby, look at that. We see your first parameter in action. Abby, Abby, Abby, and then our second one comes in right here. Eyes instead of beard. That’s just fun. That’s just so cool. But the fun isn’t over yet, our bash Butler best day ever, a script isn’t done, he can be and do so much more.
So using the skills we’ve learned so far, let’s take it to the next level. Let’s go. Let’s jump back into our script. Nano best day so far, we’ve set variables equal to simple things like text or strings. That’s what we call text in the scripting world. But this is crazy powerful. You can also have variables equal the of commands. What does that mean? Let me show you. So actually let’s get out of our script. Sorry I made you do this. Needlessly hit control X. Let’s learn a few quick commands real quick to figure out who you’re logged in as like, who am I type in one word who am I? That’s the command. So fun. You are root. You’re logged in as root. If you set up Le node, you might be somebody else. That is your username. Another fun one is to figure out, Hey, where am I in the file system?
Am I in my home directory? Am I on the desktop? Am I in downloads? We can figure that out real quick with the P WD command print, working directory, bam. Hey, we’re in the root directory. Cool. And one more fun one if you’re like, Hey, what day is it today? Just type in date. The command is date bam and tells you. So now what we’re gonna do is take the output of these commands and put them inside a variable that we can use in our script. Let’s try it out. Okay. Let’s go. Let’s jump back into our script nano best day ever about sh first thing we’ll do is set our variables. So up here, just under compliment, we’ll create a variable called user and we’ll have that equal. The who am I command? And it’s a bit different than how we’ve done it before. We’ll use the dollar sign here. I know this is kind of new. We’ll do pro parenthesis, open parenthesis and the command who am I? And then close that parenthesis. This is the format we’ll use. We want to make sure a variable will equal the output of command. So go ahead and do that for the date command and the PWD command. Do it right now. Pause the video
On pause, coffee break, and let’s do it. I’ll create able date and have that equal the command date. Easy enough. Now I’ll create the variable. Where am I? And have it equal to the command. P w D cool. Now time to use our variables in our script, and you can do this yourself. It could be whatever you wanted to do. Just something fun. I wanna say down here, sleep two, sleep for two seconds. And I’ll echo, you are currently logged in as, and we’ll use our first variable dollar sign user. And you are in the directory dollar sign. I’ll say, where am I? They’ll say? Yeah. Also today is, and then I’ll use my variable date. They’ll use a quote at the end here to close that up. And right now that’s a pretty heavy sentence, right? Like we’re echoing a lot of things using our user variable.
Our, where am I variable and our date variable. What do you say? We try it out. Let’s do it. Control X Y enter to save. I’m gonna do control L pre screen, and then we’ll run our variable keeping in mind. We have to use our positional parameters too. Read slash best day ever. I’ll say Bob and ice cream. Let’s try it out. Ready, set, go. And then yes, the commands. We are harnessing the power of the commands and using them in our script and variables. We’re currently logged in as the root user. We’re in the root directory and is this at a certain time? Whatever. So not only is our script just encouraging the heck out of us, making us have a great day from the get go. It’s also informing us about things. Speaking of which we can do more. Like we can go down a rabbit hole and get a little crazy.
Do you ever do that? That’s what I do then that’s what we’re gonna do for the rest of this video. We’re gonna go down a little crazy rabbit hole and make this a little extra. Is that okay? Let’s do it. You know, actually we’re gonna stop there because the video’s already too long, about 14 minutes, but don’t worry. I still do go down that crazy rabbit hole. In fact, this is the end result of the script. It’s kind of crazy when you run the script. In addition to what we already covered, it’s gonna tell you your private IP address, your public IP, address, the weather of a city of your choosing and the dad joke of the day. That’s how you have a great day. If you wanna check it out, I do have a link alone, the description to network Chuck academy, that video will live there anyways, back to Chuck in the past to close out this video, back to you, Chuck, and also have you hacked the YouTube algorithm today. Let’s make sure you do hit that light, but notification bell COMT subscribe the, say it all. Yes. You gotta hack YouTube today. Ethically. Of course. Yeah. That’s all I have. I’ll get you guys next time.

Check Out Network Chuck's Coffee and MERCH Shop