What is a Bash Script and How to Write It in Linux
Suppose you have a series ofLinux commands, which you need to repeat multiple times. You can either write the commands 1,000 times and curse your fate, or you can write a small and simple bash script in Linux to automate this tedious task. In this article, we will discuss what is a bash script, its advantages, and how to write a bash script in Linux.
What is Bash Script in Linux
Just like a movie script gives actors an idea of how to act out a particular scene. In the same way, a Bash script is a file containing aseries of Linux commandswhich when executed, work like they have been typed in the shell prompt, thereby, automating your workflow. All bash scripts have a .sh extension, a “shebang line” –#/bin/bash, and can be directly run via the shell prompt.
In short, a bash script groups all the commands you need to execute individually for a particular task. Now that you know what is a bash script in Linux, let us now see its advantages.
Advantages of Using Bash Scripts in Linux
Bash script in general is a powerful and versatile tool making it an integral part of every system administrator, developer, and power user’s arsenal. Here are some key advantages of using bash scripts:
Basic Bash Script Components in Linux
If you look around, almost every bash script is composed of some common yet fundamental components that act as building blocks. These blocks add functionality structure to the entire script:
1. Shebang (#!)
Every bash script starts with a “Shebang line,” which specifies the path for the Linux interpreter that will be used. It is written as:
2. Comments in Bash Script
Even though comments are simply ignored by the interpreter when the bash script is executed. They are simply used to provide some extra information about the code for its users. It also improves the overall readability of the code. To write a comment in a bash script, simply add an octothorpe (#) at the beginning of the line.
For example:
3. Variables
Variables in bash scripts help in storing different types of data temporarily in the memory. To assign a variable with a value in a bash script, use this syntax:
For example, to store “John” inside the variable “name”:
To access the value stored inside the “name” variable, simply add a “$” symbol in front:
Here, the value stored inside the “name” variable gets plugged into the echo statement which then prints the final output as:
4. echo Command
The echo command is a simple yet useful command which is used to print values as output. The syntax to print using the echo command is:
5. If-Else in Bash Script
Sometimes you may need to take situational decisions depending on different conditions. In bash, you can take different decisions withif,thenandelsecommands with the following syntax:
Here are the different character sets that you can use in bash for conditional scripts:
For example, to check if two values in a variable are equal or not:
6. Loops
Loops are used when you need to repeat a set of statements or commands multiple times. In bash scripting, you can use either a “For Loop” or a “While Loop” for your looping needs.
A “For Loop” is a kind of looping structure that is used when you know exactly how many times you need to repeat a group of statements or use it to go through a list of things. The syntax to use the for loop in a Linux bash script is –
Here’s a breakdown of the above syntax:
For example, to print all integers between 1 and 5:
In general, while loops are used when you don’t know exactly how many times you need to repeat a particular set of statements. They will continue to repeat the statements until a particular set condition is met. The syntax to use the while loop is –
In the above syntax:
For example, to print all integers between 1 and 5:
How to Write a Bash Script in Linux
Now that you know about the basic building blocks of bash scripting in Linux, we can now move on to how to write a bash script for your Linux system.
Step 1: Create a New File
You can create a new file with any of thetop text editors available for Linux. Make sure to give the extension to the script file as .sh. For example, to create the test named “test.sh” with thenano text editor.
Step 2: Define the Shebang Line
A shebang line defines the path to the interpreter which you want to use for executing the bash script. It is the first line of every bash script and is written commonly as:
OR
If you need to use any other interpreter for your script, use this syntax:
Note: It is not compulsory to add comments to every bash script you write, but they can improve the overall readability of the code. Use the octothorpe symbol to add comments to your script. Once the file gets executed, all comments get ignored by the interpreter.
Step 3: Add the Commands and Code
Now you can start adding the necessary commands and instructions with the help of bash components, all combined in a logical manner. Let’s say you need to automate the process of moving all the .txt files in a directory to a separate directory. Use this bash script to move all .txt files into a separate directory in your Linux system:
Seems complicated? Let’s break it down into parts:
Step 4: Save the Script
Once you have finished working on the bash script, save the file and exit the editor.
Step 5: Make the File Executable
Generally, all script files are not executable by alltypes of users in Linux. To make the bash script executable, use the following command:
For example, to make the file test.sh as executable:
Step 6: Execute the File
Once you have made the bash script as executable, run the file with the following syntax:
To run the test.sh file:
Beebom Staff
Bringing the latest in technology, gaming, and entertainment is our superhero team of staff writers. They have a keen eye for latest stories, happenings, and even memes for tech enthusiasts.
Add new comment
Name
Email ID
Δ