Shell scripts are a great way to automate repetitive tasks on Linux. You can write Bash scripts that perform system-related tasks such as installing software, adding new users, dynamically configuring the desktop, just to name a few.
But what’s the prerequisite? You should have in-depth knowledge of the Bash shell and its commands, including how to wrap these commands in a script—and the most important—how to run the script.
Here’s how you can create and execute Bash scripts on Linux.
What Is Bash Scripting?
A script is a sequence of commands intended to perform a specific operation, which would otherwise be done manually by a user. Generally, the commands included in a script are related to a shell, and the umbrella term for writing such scripts is “shell scripting.”
Linux boasts a variety of shells that users can install and configure. The most prominent ones are Bash, Zsh, and fish; writing a script comprising Bash commands is known as Bash scripting.
Apart from Bash scripts, there are several other types of scripts as well. For example, Python scripts, Ruby scripts, Perl scripts, PHP scripts, etc.
How to Create a Bash Script on Linux
If you want to learn how to write Bash scripts on Linux, all you need is a text editor (and a dash of persistence).
Creating a New File
To get started with scripting, create a new file with the extension “.sh”. You can do so easily using the touch command.
Open the newly created file with any text editor of your choice. It can be a GUI-based editor like Visual Studio Code or a terminal-based one like Vim or nano.
To edit the file with Vim, run the following command:
Writing a Bash script is as simple as adding words to a text file. But of course, you need to know which words are valid (interpreted by the command shell), and which aren’t.
For the purpose of this guide, let’s add the following code to the script, which displays your username and the specified string on execution.
echo "Hello World"
Once you’re done with writing the commands, save and exit the file to proceed.
Since a Bash script is a collection of Linux commands, any command you run in the terminal can be included in the script. Some examples include find, grep, man, ls, cd, etc.
How to Execute the Bash Script
Unlike other scripting languages, you don’t need to install a compiler (or interpreter) for Bash. Every Linux distro ships with the Bash shell by default, and as a result, has everything you need to execute your scripts.
From the Terminal
The most common way to run Bash scripts is using the terminal. All you have to do is grant execute permissions to the script file using the chmod command.
sudo chmod +x script.sh
Now, to execute the script, run:
The output will display your username and the string “Hello World,” as specified in the script file.
Using the GUI
If you’re repelled by the idea of using the command line and want a graphical approach to the whole execution process, well, you’re in luck.
Similar to what we did before, you’ll have to grant execute permissions to the file first. To do that, right-click the file and select Properties from the context menu. Locate the option that says Execute or Is executable and check off the box next to it, or enable it, depending on the desktop environment you’re using.
Then, simply double-click the script file and select Run in the terminal or a similar option to execute the Bash script.
The Power of Bash Scripting
Most command-line utilities on Linux make use of scripts, which are a sequence of commands that execute in a particular order to perform a specific task. Knowledge of shell scripting can be a great addition to your technical skillset, and makes automating tasks way easier for you, as a Linux power user.
In addition to being able to accept and execute commands interactively, the shell can also execute commands stored in a file. This is known as shell scripting. Here we cover the basics of shell scripting.
About The Author