Your First Bash Script!

Austin Newton
4 min readJul 26, 2023

--

Prerequisites:

  • Your Ubuntu Machine
  • Internet Connection

Intro

Hello Everyone,

This post follows my previous post ‘Keeping Ubuntu Up-to-Date’. If you haven’t already, check that out.

We left off the last post by updating Ubuntu and all our other packages. If you remember, just doing a simple update of your packages and OS can be quite involved for the task that it is.

Enter Bash scripting. Imagine a Bash script as a list of instructions. When we create and run a Bash script, you can tell your machine to run a list of commands in a row. So instead of typing each of the commands from the previous post, every time we login to our Ubuntu box, we can type one command to run our Bash script and all of the commands will be ran.

Enter Bash Scripting

Start by logging into your Linux machine and opening up the CLI.

Type the command:

ls

You should see something like this. This is a list of your current users home directory. As you can see this is where you would store your personal files.

sudo touch updateplease.sh

What is this command? This command creates a blank text file named ‘updateplease.sh’, in our current directory (Folder). This will be our Bash script.

Note: If you would like to name it something else, just replace the name updateplease and replace it with the name you choose. Careful, Linux is case-sensitive.

Now go ahead and run the

ls

command again.

See the new file we just created?

Lets go ahead and enter the commands inside our Bash script.

sudo nano updateplease.sh

Once you enter this command your will be placed inside the Nano text editor. It should look something like this:

As you can see there is nothing in this file. We will start off the bash script with the following:

#! /bin/bash

This string of text lets our device know, ‘Hey, this is going to be a Bash Script’.

Next we will enter our commands from earlier like so:

#! /bin/bash
sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get dist-upgrade -y
sudo apt-get autoremove -y

I know what your saying, ‘whoa whoa whoa, what are the -y’s for?’ Remember when we had to confirm our decision by hitting ‘y’ for yes? This will automagically tell the prompt ‘y’ when it comes up.

When you are ready to save and close the Bash script, hit the keys:

Ctrl + X

At the same time.

It will then prompt you to say ‘y’ for yes to save the file. If this was done correctly we will end up back in our CLI.

Next enter the following:

sudo chmod u+x updateplease.sh

Taking the time to fully explain this command will go outside the scope of this tutorial. In short, this command allows your user account to execute the Bash script we just created.

After you hit enter on this command, nothing should have changed.

To make sure the changes were successful, enter:

ls

Notice how our Bash script is now green. This means it can be executed. So how do we execute it?

Enter the following command:

sudo ./updateplease.sh

BOOM!

Once you hit enter you will see a wall of text scrolling past you once again. Once the script is finished it will give you back control of the CLI and voila, you have created your first Bash Script. For good practice, go head and run the script every time you login to your Ubuntu machine.

--

--

Austin Newton

IT Professional with a passion for networking and systems.