Introduction to bash scripting

If you use a Linux or Mac computer, or if you are a developer, chances are you will work with the terminal at some point. The most common command line application that is used is bash. This post is to get you familiarized with one of the most powerful features of bash which is bash scripting.

Why make bash scripts?

There are many reasons why bash scripting is so strong. For starters, if there is a task that you find yourself doing over and over again until it becomes tedious, you could create a bash script that would do it for you in an instant. Why repeat the same work when you could automate it? And the best part is that it’s not as hard as it may sound. This post will attempt to get you to create your first bash script, and to understand how it works.

Let’s start:

I am going to assume that you have access to a bash terminal window. If you are using Windows you might want to either install a bash for windows program, or to run a Linux Operating System through Virtual Box. Anyhow, once you in bash, go ahead and create a new file with the editor of your choice. You can use vim, nano, or even a gui editor if you would like. just make sure you are making the file in the same directory where your bash terminal currently is. You can check your current directory with the commands

$ pwd

I am going to create a new file using the vim editor

vim mybashscript.sh

Note that bash script files don’t need an extension. By convention we use .sh to easily tell it’s a bash script file, but I may as well have named the file mybashscript and it would have made not difference.

once the editor is open, and we are ready to start typing (if you are using vim to edit make sure you press “i” to enter insert mode), you want to make sure the first line of the file is

#!/bin/bash

This line tells your computer that the content of this file is a bash script (hence why no file extension is needed).

Go ahead and press enter once or twice and type:

echo "This is my first script"

Now your entire file should look like the following

#!/bin/bash
echo "This is my first script"

Now save the file (if you are using vim you want to press   esc  then  :  followed by wq)

Once you exit the editor, make sure the file was created by typing the command:

$ ls

which the files sitting in current directory.

You should be able to see the file named mybashscript.sh

At this point we are not ready to execute the script yet, and this is because whenever you create a new file, by default it will not have execute permission. To add execute permission to the file we can use the chmod command:

$ chmod +x mybashscript.sh

This will make it so that now we can execute the script file.

To run the file, we have to type into the terminal

./mybashscript.sh

The reason we have to type ./ in front of the file name is that bash doesn’t know that mybashscript.sh is a global command. When you make a script you have the option of making your script a command that you can run from anywhere just like the command ls, or pwd. By typing the ./ we are telling bash that the file we want to execute is located in the current working directory which is what the dot in (./) stands for. After typing ./mybashscript.sh into the terminal and pressing enter, you will notice that the text (This is my first script) appears in the terminal which signifies our script ran successfully and did what we asked it to do. Our script is a very basic script that only does one thing (print some text to the screen), but we could have added more commands to the script which would have executed sequentially one by one. Congratulations, you just made and ran your first bash script.

One more thing. If you want your script to be recognized as a global command so that you can just type myfirstscript.sh into the terminal from any directory and it executes properly, you will have to add the directory that contains the script file to the $PATH environment variable and this is something that will be covered in a future post.

If you liked this tutorial or if you have any feedback, please leave a comment.

 

Leave a Reply

Your email address will not be published. Required fields are marked *