Getting started with BASH scripting- Part 1

Getting started with BASH scripting- Part 1

what is bash scripting

What is the use of bash scripting?

Bash scripting allows an user to group a bunch of commands together and execute it. This will be tuturial series explaining one core component on each post. In this post, we will be looking at how to write a simple bash script and how to execute it.

Some basic terminologies before we dive deep into bash scripting.

What is a Shell?

Shell at it's core is what makes a linux system unique in it's own way. There are a ton of powerful features available in a linux environment and all these features are brought to life with the help of a shell. A shell is "a program taht interprets the commands you type in your terminal and passes them onto the operating system. " Whenever you log into your system and open a terminal in your system, your are in turn interacting with a shell. As you might know, the kernel is the heart of any operating system and the shell provides you access to the full features of the kernel. Basically, a shell is a program, that acts as an interface between a user and a kernel. Through the shell, you are able to utilize the features of the kernel and other programs in your system. Without a shell, you wouldn't be able to do even basic tasks in your system.

So, does that mean there exists different kinds of shells?

Yes, that is correct. There are several types of shells available, where each one of them serves a different purpose according to the task at hand. The following are the different kinds of shells that are available in linux:

  1. The Bourne Shell (sh)
  2. The Bourne-Again Shell (bash)
  3. The C shell (csh)
  4. The Korn Shell (ksh)
  5. The Z Shell (zsh)

As our title suggests, we will be discussing the bash shell in depth and how to write scripts using bash.

What is BASH? BASH stands for the Bourne-again shell. Bash is actually based on the bourne shell, created by Stephen Bourne in 1979. Since BASH is an improved version of the original bourne shell, the name bourne-again shell makes perfect sense. Bash is arguably one of the most commonly used linux shells in use today. The main reasons for this popularity is because of its rich features, fast interface and its very commonly used.

Then again, What are shell scripts?

A shell script is a plain text file that contains a set of commands that is designed to run within the shell. As we explained above that there are different types of shell, and each shell has its own scripting language. Bash is the default shell that comes with Linux and we will be therefore writing bash scripts which is a shell script. The shell reads these scripts and executes each commands line by line.

OK, enough talk, lets write a basic bash script

First of all, you need to create the file for a bash script which we can later modify and add our needed commands.

$ touch hello.sh

now open the file in the editor that you are familiar with. In this tutorial, I will be using vim

$ vim hello.sh

Now, write the following piece of code into the that newly opened file. It's perfectly ok if you dont understand a single line of code, we will be explaining it line by line.

#!/bin/bash

ls
mkdir new-folder
echo "Hello World"

exit 0

structure of a bash script

A bash script can be summarized into 3 main parts, a beginning, a core and the end part. The Beginning (shebang line) Every bash script starts with what is known as the shebang line #!/bin/bash. This should be the first line of your bash script and there should be no blank lines or spaces before this line. The shebang line tells the shell what language this script is written in. In our case it's bash and after the #! we are providing the path to the interpreter that is used to read our file. And that interpretor is bash and is located at /bin/bash

The core ( the commands)

The 3 commands ls, mkdir and echo are some of the commands that we use on a daily basis. In fact, these 3 basic commands will get executed line by line starting from the first ls, then the mkdir gets executed and then the hello world will the displayed on the terminal.

The end (exit statement)

At the last line, we are exiting with an exit code of 0. This is generally not required and there are very few chances for error in our above script. To give an exit code to the shell, we use the exit command and a code 0 to indicate everything went well. When the script reaches the line exit 0, the script will end and give an exit code 0 back to the shell.

TIP

What happens if you don't provide an exit code? If you chose not to provide an exit status, the shell will simply take the exit code of the last command that you ran as the final exit code of your script.

how to add comments in your scripts

Comments can be really helpful when you are writing bash scripts. Like in other programming languages, you can make use of commenting your code so that you can understand it better later. Also, commenting makes the code easier for others to understand your code. Comments are never read by the interpretor and it's for the humans only.

#!/bin/bash

# Author: Sebin Sebastian
# Date Created: 27/08/2022

# Description 
# This is my first script in bash 

# Usage
# ./hello.sh

ls
mkdir new-folder
echo "Hello World"

exit 0

In bash, comments are inserted using a simple # symbol at the beginning of the line. When you start a line with a hash symbol, the shell knows that the line contains a comment and it will simply skip that line and move onto the next line. The author, date, description and usage is really useful for a bash script. It makes your bash script more professional and it makes it really easier to understand your script when it is read by others.

#now what, I wrote my first script and how can I run it? Yes, now the only step remaining is to execute our shell script and see the output of the script. In order to execute something in the linux environment, there exists certain permissions for files. You can only execute a file if you have execute permissions for that particular file. so we will be changing permissions for that file using the following command

image.png As you can see, after the firstr rw there is no x which means there is no execute permissions for our file.

$ chmod +x hello.sh

image.png As you can see now, the file permissions for the user have changed to rwx and the colour of the file has also changed. This indicates that the file can now be executed.

This will grant execute permission for the script file hello.sh and now we can execute the file. The file can be executed with as follows:

$./hello.sh

The output in the command line after executing our script is as follows:

image.png The ouput of the first command ls -alh is the first four lines, and the second command mkdir new-folder creates a new folder and lastly, its prints hello world using the echo command.

Summary

You learned all about shells, scripts and what bash scripting is. You learned that the bash scripts have 3 core components, which is essentialy the shebang line, the command part and the exit statement. If you know more commands, the more powerful your bash scripts can become. You also learned to run your bash scripts by giving them execute permissions.