If you mention bash scripting to any tech admin or developer, you’ll be greeted with nods of approval or a rousing “Been there, done that.” The reason for this is that bash scripts are an incredibly handy tool that helps to make the Linux operating system flexible, powerful, and useful.
But what are bash scripts and why should you be using them?
These questions are often asked and easily answered. But before you hear the explanation, you probably should first know what bash is. After all, you probably already know what a script is, so putting this two and two together starts at the terminal itself.
What is Bash?
Simply put, bash is a shell and command language that’s used in Linux. Okay, that’s great, but what is a shell? In the realm of Linux, the shell is a command-line interpreter that provides a user interface for running commands. Without the shell, you couldn’t run commands. Without commands, there’d be no operating system. Without an operating system, there’d be no PC.
So, bash is a type of shell that makes it possible to run commands on Linux.
But what does that 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 run within the shell (in this case, bash). Every shell has its own scripting language and, since bash is the default shell with Linux, most development and admin teams use the bash scripting language.
With these scripts, you can do almost anything. And like Python, they’re fairly easy to learn and don’t require a compiler to use.
To really understand shell scripting, you first have to understand how to use the command line in Linux. Let’s take a look at a very simple example. Let’s say you want to print out the following phrase:
The date and time is
Following the above, you want to print out the actual date and time, using the date command. This would be done with:
echo "The date and time is " $(date)
Hit enter and the output will be something like this:
The date and time is Wed Jan 19 09:04:16 AM EST 2022
We’ll now see how this is done with a bash script.
How to Create a Bash Script
The first thing you must know is that every bash script starts with:
Create a new file with the command:
Add #!/bin/bash to the top of the script, which instructs the terminal everything found within the file is a part of a script. After that, we can create our program with just a few lines. The first thing we’ll do is format the date as we like it using a variable like this:
The above will display the date in the format of the date (Month, Day, Year) and then the time (Hour, Minutes, Seconds). We’ll use that variable (formatted_date) in conjunction with our echo command in the line:
echo "The date and time is " $formatted_date
Save and close the file and give the script executable permissions with:
chmod u+x date.sh
If you run the command ./date.sh, the output will look similar to when you ran the echo command from the terminal window.
And that’s how you build a bash script.
Why You Need Bash Scripts
Now that you understand the very basics of how bash scripts work, it’s important to know that bash scripts can be anything from simple one or two-line applications to incredibly complex apps that can do just about anything (from backing up directories to external drives, to running ping tests for site uptime, monitoring server performance, and customized administrative tasks. In fact, there’s very little bash scripts can’t do (so long as you don’t need a GUI).
One major benefit of using bash scripts is that they can be created with very little (to zero) programming knowledge. That means you can create non-GUI Linux applications without first having to learn how to program.
If you can run a command, you can write a script.
And given how flexible bash scripts are, you can actually get quite a bit of mileage out of them, before you run up against a wall and have to turn to more traditional programming skills.
Pros and Cons of Bash Scripts
There are several advantages to using bash scripts and only a few disadvantages.
Advantages of Bash Scripts
The advantages of bash scripts include:
- Simple to create
- Can run multiple commands easily
- Interactive debugging
- Can be automated
- Installed on nearly all Linux operating systems
Disadvantages of Bash Scripts
Bash scripting isn’t without it’s disadvantages, which include:
- Not nearly as fast as a standard program
- Limited usability (when compared to traditional programs)
- Launches a new process for every shell command executed (which means it can be resource intensive, if a script is complex)
The longer your admins work with the Linux operating system, they’ll eventually turn to a shell script. In fact, you should encourage them to start with shell scripting to take on smaller tasks and then migrate to traditional development when they run into the inevitable wall.