Understanding the Bash:
As it has already been stated in this article, that Bash is a flavor of Shell that is very commonly used in the Linux operating system. Now let us try to understand what it is and what is the need of using it. In most of the flavors of the Linux operating system, Bash is the default command-line interpreter. It means that you do not have to explicitly enable it to use it because it has been enabled by default. Bash, in fact, stands for Bourne-Again SHell.
Whenever you launch the terminal in Linux operating system, the default program that runs inside it is Shell or Bash. This default program is responsible for interpreting all the commands that you run on the Linux operating system. We can also say that Bash is responsible for running the other programs in the Linux operating system. On the other hand, the Linux terminal acts as a bridge between Bash and the user and enables a smooth interaction between them.
Since Bash is responsible for executing the commands that you run in the Linux terminal, therefore, many of the important tasks can be associated with it as its responsibilities. It can be used to configure different programs or even run them via terminal. It can manipulate the files stored on your Linux operating system. It is also capable of interacting with databases. Apart from this, Bash is also a full-fledged scripting language and we will try to find out how is it possible in the next section of this article.
What are Bash Scripts?
A Bash script refers to a text file that contains a set of different commands that together make up a program or a script and their job is to tell Bash whatever it is supposed to do. The main motive behind creating Bash scripts is to give rise to usability. Whenever we want to perform any task via the terminal, we type in the respective command for it. However, whenever we are supposed to perform that task again, we will have to type in the very same command again and for as many times as we want to perform that task.
Can you realize the problem in this situation? Yes, you got it right. For the very same task, we have to write the same command again and again if we wish to perform that task repeatedly. How about if we store that command or a series of such commands in the form of a file and instead of typing those commands, again and again, we can simply run that file whenever we want to? That is exactly where the Bash scripts or Bash scripting comes into play. The Bash script files have an extension of .sh e.g. MyFirstBashScript.sh. Here, MyFirstBashScript is the name of the file whereas .sh is its extension.
You can also set the read, write, or execute permissions for the Bash scripts for ensuring access control. If a file does not have the execute permission set, then you will not be able to run it in any way. Generally, the system files are read-only i.e. you cannot write or modify them for ensuring proper system functionality. The user files are writable only by authorized users which can be specified either as individuals or in the form of a group.
Benefits of using Bash Scripts:
Following are some of the mainstream benefits of using Bash scripts rather than running commands directly via the Linux terminal:
- Bash scripts allow you to automate the commonly occurring or repetitive tasks.
- They are efficient to use.
- They save you from memorizing the commands for a long time since once you will write them in the text file, they will stay there forever and you will not have to recall them every time you want to run those commands.
- The Bash scripts are as easy to use as the terminal commands.
Examples of Bash Scripts:
In this section of the article, we are interested in exploring some very basic programs while making use of the Bash scripts. In this way, we will not only be able to learn Bash scripting but we can also save our time in the longer run. So let us get started.
Note: We have demonstrated all these examples while using Linux Mint 20. However, you are free to choose any flavor of Linux according to your preference.
Hello World Bash Script:
If you are a programmer or even if you have the very slightest knowledge of computer programming, then you must know that whenever we are learning any new programming language, the very first program that we write is a “Hello World” program. So today, we will first learn the method of writing a “Hello World” program in the form of a Bash script. For doing that, you will have to step ahead as explained below:
Create a file named HelloWorld.sh by right-clicking anywhere on your Desktop and then choosing the New Document option from the cascading menu and Empty Document option from the sub-cascading menu. You can have any name of your choice for this file. For this example, I have created this file on the desktop as shown in the following image:
Now double click on the file to open it and type “#!/bin/bash” as the very first line of this file. This line is added to indicate that the current document is a Bash script as shown in the image below:
After doing this, type the following line in your file:
The echo command is used to display whatever is written after it. After typing in this line, save your file and close it.
Now launch the terminal in Linux Mint 20 by clicking on its icon and navigate to the desktop by typing the following command and pressing the Enter key:
We have performed this step solely because our Bash script resides on the desktop. If your Bash script is saved in the Home folder, then you can skip this step. Once you have done this, type the following command in your terminal:
Here, you can replace HelloWorld with whatever name you have given to your Bash script.
As soon as you will press the Enter key to execute this command, the contents of your Bash script will be displayed on the terminal as shown in the image below:
Bash Script for Taking User Input:
At times, we do not simply want to display some messages to the user rather we want to take his input maybe for calculating some results or for any further processing. So, for writing a Bash script that is capable of taking input from the user, you will have to have to go through the following steps:
Create a file in the same manner as explained above and name it as UserInput.sh. You can also give a name of your choice to this file. It is shown in the following image:
Double click on the file to launch it and then type “#!/bin/bash” in it as shown in the image below:
Now type the text shown in the following image in your Bash script. Here, the read command is used to take user input whereas num is the variable in which the user input is stored. Moreover, whenever we want to manipulate this variable, we will have to use the “$” symbol before it.
Save and close your file and then launch the terminal. Navigate to the directory wherever your Bash script is stored and then type the following command in your terminal:
Here, you can replace UserInput with whatever the name you have given to your Bash script.
As soon as you will run this command, the terminal will prompt you to provide the requested input as evident from the following image:
You can enter any number of your choice. However, in this example, I have added number 27 as shown in the image below:
When you will press the Enter key, the next line of the Bash file will execute which will reflect the input that you have provided by displaying it on the terminal as shown in the following image:
Applying Conditions while Using Bash Scripts:
Whenever we want to specify two different execution paths, the very first thing that comes in the mind of a programmer is the “if-else” statement. The purpose of the “if-else” statement is to provide two or more different execution paths depending upon the processing results or the inputs provided by the user. Therefore, in this example, we will try to learn the method of using “if-else” statements in Bash scripts. The detailed method is explained below:
Create a file named Conditions.sh in Linux Mint 20. You can name this file according to your own choice. For the sake of demonstration, we have created this file on the desktop as shown in the following image:
Double click on this file to open it and then type “#!/bin/bash” in it as shown in the image below:
Now copy the script shown in the following image in your text file for using the if-else statement in Bash script. Here, -eq is the equivalent of “==” comparator that we normally use in programming to check the equality of two variables or numbers. Save your file and then close it.
Launch the terminal in Linux Mint 20 and navigate to the directory where your Bash script resides. Type the following command in your terminal for executing your Bash script:
Here, you can replace Conditions with whatever name you have given to your Bash script.
As soon as you will execute this script, you will be asked to type a number i.e. either 0 or 1 as shown in the image below:
You can enter either of these numbers according to your choice. In this example, I have added 1 which will ideally indicate that the switch is on.
As soon as you will press the Enter key after providing your input, the output will verify that the switch is on since the else part of the script is executed. This can be verified from the following image:
To test whether the if part of the statement also works fine or not, we ran the same script once again and this time, we provided the input 0 which means that ideally, the if part of the statement should be executed. You can verify from the image shown below that our conditional Bash script is working exactly as it was intended to.
In the very same manner, you can create more complex Bash scripts for automating your routine jobs.
This guide gives you a deep insight into Bash and Bash scripting. It explains to you the need of using Bash scripts along with an emphasis on their benefits. It even teaches you the basic syntax of Bash scripting by demonstrating some good examples. So, now we can easily hope that you are in a good position of getting started with the Bash scripting and using it for your daily activities. This will not only make your programs all the more efficient but it will also offload the burden of memorizing complex commands from your shoulders every time you wish to use them. Once you write a Bash script, you can use it for as many times as you want without having to recall that complex code.