Shell scripts are a great way to automate repetitive tasks in Linux. You can write Bash scripts that perform system related tasks like installing software, adding new users, dynamic desktop setup, to name a few.
But what is the prerequisite? You should have a thorough knowledge of the Bash shell and its commands, including how to encapsulate these commands in a script – and most importantly – how to run the script.
Here’s how you can create and run Bash scripts on Linux.
What is the Bash script?
A script is a sequence of commands intended to perform a specific operation, which would otherwise be performed manually by a user. Usually, the commands included in a script are linked to a shell, and the generic term for writing such scripts is “shell script”.
Linux offers a variety of shells that users can install and configure. The most important are Bash, Zsh and fish; writing a script that includes Bash commands is known as a Bash script.
Besides 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 hint of persistence).
Creating a new file
To start with the scripts, create a new file with the “.sh” extension. You can do this easily by using the touch control.
Open the newly created file with any text editor of your choice. This can be a GUI based editor like Visual Studio Code or a terminal based editor like Vim or nano.
To edit the file with Vim, run the following command:
Writing a Bash script is as easy 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 are not.
For the purposes of this guide, let’s add the following code to the script, which displays your username and the specified string at runtime.
echo "Hello World"
Once you are done writing the commands, save and exit the file to continue.
Because 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 run the Bash script
Unlike other scripting languages, you don’t need to install a compiler (or interpreter) for Bash. Every Linux distribution ships with the default Bash shell and therefore has everything you need to run your scripts.
From the terminal
The most common way to run Bash scripts is through the terminal. All you need to do is grant execute permissions to the script file using the chmod command.
sudo chmod +x script.sh
Now to run the script run:
The output will show your username and the string “Hello World”, as specified in the script file.
Using the graphical interface
If the idea of ââusing the command line turns you off and you want a graphical approach to the entire execution process, well, you’re in luck.
Similar to what we have done before, you will need to grant execute permissions on the file first. To do this, right-click on the file and select Properties from the context menu. Locate the option that says Execute Where is executable and check the box next to it or enable it, depending on the desktop environment you are using.
Then just double click on the script file and select Run in the terminal or a similar option to run the Bash script.
The power of Bash scripts
Most command line utilities on Linux use scripts, which are a sequence of commands that run in a particular order to perform a specific task. Knowledge of shell scripting can be a great addition to your technical skills and makes automating tasks much easier for you as an experienced Linux user.
In addition to being able to accept and execute commands interactively, the shell can also execute commands stored in a file. This is called shell scripts. Here we cover the basics of shell scripts.
About the Author