What Are Bash Scripts and Why Should You Use Them?
If you mention bash scripts in a conversation with any technical administrator or developer, you will be met with approving nods or exclamations of "Been there, done that." The reason is that bash scripts are an incredibly convenient tool that helps make the Linux operating system flexible, powerful, and useful for software development.
But what are bash scripts and why use them? These questions are often asked and easy to answer. But before hearing the explanation, you probably should first learn what bash is. After all, you probably already know what a script is, so you need to start putting two and two together from the terminal itself.
What is Bash?
Simply put, bash is a shell and command language used in Linux. Okay, that's great, but what is a shell? In Linux, a shell is a command-line interpreter that provides a user interface for executing commands. Without a shell, you cannot execute commands. Without commands, there would be no operating system. Without an operating system, there would be no PC. So, bash is a type of shell that allows you to execute commands in Linux. But what does this have to do with your development team? Two words: Shell scripts.

What are Shell Scripts?
Now we come to the heart of the matter. A shell script is a computer program designed to be executed within a shell (in this case, bash). Each shell has its own scripting language, and since bash is the default shell in Linux, most development and administrative teams use the bash scripting language. With these scripts, you can do almost anything. Like Python, they are fairly easy to learn and do not require a compiler. To understand what shell scripts are, you first need to understand how to use the command line in Linux. Let's look at a very simple example. Suppose you want to print the following phrase:
The date and time is
After that, you need to output the actual date and time using the date command. This can be done as follows:
echo "The date and time is " $(date)
Press Enter, and the result will be something like this:
The date and time is Wed Jan 19 09:04:16 AM EST 2023
Now let's look at how this is done using a bash script.
How to Create a Bash Script
The first thing you should know is that every bash script starts with:
#!/bin/bash
Create a new file with the command:
nano date.sh
Add #!/bin/bash to the beginning of the script, which tells the terminal that everything in the file is part of the script. After that, we can create our program in just a few lines. The first thing we will do is format the date as we like, using a variable as follows:
formatted_date=$(date +%m_%d_%y-%H.%M.%S)
The above will output the date in numeric format (month, day, year), followed by time (hour, minutes, seconds). We will use this variable (formatted_date) together with our echo command in the line:
echo "The date and time is " $formatted_date
Save and close the file and give the script execute permissions:
chmod u+x date.sh
If you run the command ./date.sh, the result will look the same as when executing the echo command from the terminal window. That's how a bash script is created.
Why Are Bash Scripts Needed?
Now that you understand the basics of bash scripts, it is important to know that bash scripts can be as simple as one- or two-line applications, or incredibly complex programs that can do almost anything (from backing up directories to external drives to conducting ping tests to check website health, monitoring server performance, and


