Bash Scripting Basics

Bash is fundamental to Linux, and every Linux user uses it at some point or another. This post will help you understand bash scripting basics and provide some pro-tips!

Bash is an acronym for “Bourne-Again SHell” and a command language interpreter interprets our commands. Bash is a default interpreter on most of the GNU/Linux-based systems. We can check the default interpreter in our system by the following command:

echo $SHELL

What is a bash script?

A Bash script is just a set of commands we regularly use in a terminal, written in a sequence. You can use Bash scripts in many different ways. For instance, you can run multiple commands together and automate tasks.

First Hello World  Bash Script:

You can use any of your favourite editors to write the script. Let’s run the following command in the terminal to open a file in nano editor:

nano script.sh

To save this script press ctrl+x and then y and then enter. Ok, so, we have created a file script.sh and wrote one command in it. Now, let’s execute this script by “bash script.sh” command.

As you can see, we have successfully run our first bash script. Again, bash in the above command is the name of the shell in which we executed our script. Of course, you can also use other available shells to run your script. For example, use the “cat /etc/shells” command to list all the possible shells on your system.

So, these are the list of available shells which we can use to run our script. For instance, we will use the “dash script.sh” command to run our script using the dash shell. Now, let’s run the script by adding multiple commands. Again, open up the script.sh file by “nano script.sh” command.

Now again, you can execute this script by the “bash script.sh” command. This was the first way of running a script.

Adding a Shebang Line to Scripts:

Now, let’s see another way of writing a bash script in which we will add a line “#!/bin/bash” at the top of the script. This line is called the Shebang. This line ensures that the Bash shell will be used to interpret this script. It always starts with the #! and is followed by an absolute path of the shell that you want to use, and in this case, because we want to use the Bash shell, we have mentioned an absolute path for the Bash shell.

You can also use an absolute path for other shells. To get the list of available shells, you can use the “cat /etc/shells” command, and from that list, you can copy an absolute path for your desired shell.

How to make a Script Executable?

By default, any newly created script is not executable regardless of its sh extension. To run any newly created script, we have to make it executable by using the chmod command which stands for “Change Mode” and in this case, we need to make it executable by running the “chmod +x script.sh” command otherwise you will get the “Permission denied” error when you run this script. 

How to Run an Executable Script?

After that, we run this script by the “./script.sh” command. Here the dot (.) refers to the current working directory. Do not forget to use the dot operator( . ) and forward-slash( / ) if your script resides in the current directory. If your script does not reside in the current working directory, you can specify the path to that directory, and the script will run just fine. Once you have made your script executable by the “chmod +x script.sh” command, you do not need to make it executable again after you make any changes in the script or if you want to rerun it.

2nd way of Executing a Script having Shebang Line:

Another way of running the above script is to use the “bash script.sh” command just like we did previously when we did not make use of the Shebang line in our script. This way you can run your script without making it executable first. Remember, it is not necessary to name your script with a sh extension, i.e., you can also name it as “script” instead of “script.sh”

You can also use “#!/usr/bin/env bash” as a Shebang line. This Shebang line works on most of the Linux and UNIX based systems.

Tips and Tricks for Writing Effective Scripts:

There are many useful tips and tricks that we can use in bash scripting.

It is a good idea to run your commands in the terminal first before you add them to your script, as this will make sure that you are writing the correct commands in your script.

Always use comments in your script. A comment in a script starts with a   # symbol. Comments are handy and can help others understand the script’s purpose and help debug the code from errors.

Always use double quotes  when referencing variables for example, on the top of this discussion we have used the following command:

echo $SHELL

Instead, it is good idea to surround this variable with double quotes:

echo “$SHELL”

Always use functions to enclose each functionality in a separate function as this will make your code more reusable and readable.

Wrapping up

Let’s summarise what we have learned:

  • You can do bash scripting via any text editor like Nano or VIM.
  • There are many shells available to run our script, and you can use the command line or shebang to specify a shell.

You may also be interested in

About Anto Online

Anto, a seasoned technologist with over two decades of experience, has traversed the tech landscape from Desktop Support Engineer to enterprise application consultant, specializing in AWS serverless technologies. He guides clients in leveraging serverless solutions while passionately exploring cutting-edge cloud concepts beyond his daily work. Anto's dedication to continuous learning, experimentation, and collaboration makes him a true inspiration, igniting others' interest in the transformative power of cloud computing.

View all posts by Anto Online

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.