One of the most powerful utilities you can use when working with Linux systems is the terminal. Here, you can execute any commands to perform any tasks you might think of – launching an application, installing/ uninstalling applications, creating and deleting files/ directories, etc. However, most users well versed with Linux systems utilize the Terminal to carry out one more task – writing and running shell scripts.
What is a shell script?
A shell script is a simple program that runs on the Unix/ Linux shell. There are different types of Shells, as you will see in the next section. A Unix/ Linux shell program interprets user commands which are either directly entered by the user or which can be read from a file that we now call a shell script. It is important to note that shell scripts are interpreted and not compiled. Therefore, when you write a script on your system, you don’t need to compile it. Just make it executable and execute it.
A shell script can be a program to perform/ automate anything on your system. For example:
- A script to install an application. That is mainly used in applications that require you to install additional libraries/ dependencies. The developers write a simple script that does all the dirty work to relieve the end-user of this hassle.
- A script to perform a daily/ weekly or monthly backup
- A script to copy specific files from one directory to another
- A script to rename multiple files
The examples listed above might sound relatively easy to implement. However, there are complex scripts that perform complicated tasks like:
- Checking for any new mails
- Scanning for vulnerabilities and security issues and much more
Let’s look at the different types of Shells.
Types of shells
Any operating system (whether Linux or Windows) comes with a Shell. However, Linux supports multiple types of Shells allowing you to run different languages or different programs for different kinds of Shells. They include:
- Sh
- Ksh
- Csh
- Restricted Shell
- Bourne Again Shell (BASH)
The sh shell
The Sh shell, commonly known as Secure Shell, was one of the earliest Shell included in the Unix/ Linux systems. That was the shell logged in by the superuser known as root. The root user could use this shell to create and delete users on the system.
The C shell (Csh)
You will undoubtedly run into the C-shell if you are a network or systems administrator working in a Linux or Unix environment. Therefore, it’s highly advisable to get familiar with this shell type. Casual users and developers will likely suggest using other shells, but the C-shell is an excellent choice if you are comfortable with the C programming language.
The Korn shell (Ksh)
The Korn shell is the one you can use interactively to execute commands from the command line or programmatically to create scripts that can automate many computer maintenance and system administration tasks.
The Bourne Again Shell (Bash)
The Bash shell is a far too big subject to be covered in a few lines. However, it’s one of the most commonly used scripting languages that you will find today, and most of the content you will find around shell scripting will be in Bash. We highly recommend learning Bash de to its versatility and ease of use.
This post will focus on Bash scripting, and the Linux distribution we will use to run the scripts is Ubuntu 20.04 LTS.
Understanding the various components of a shell script (Bash)
The first step to writing any Bash script is understanding the file extension you will use. Bash uses the ‘.sh’ file extension. Therefore, if I had a script called ‘script_one,’ I would save it as ‘script_one.sh
.’ Luckily, Bash allows you to run scripts even without the ‘.sh’ extension.
The next thing you need to understand is the Shebang line, a combination of ‘bash #
‘ and ‘bang !
followed by the bash shell path. The shebang line is written at the start of every script and specifies the path to the program to run the script (it’s a path to the bash interpreter). Below is an example of the Shebang line.
#! /usr/bin/bash
However, you might have seen other people writing is as:
#! /usr/bash
You might have noticed the difference in the path – one uses the /usr/bash
while the other uses /usr/bin/bash
. To get the bash path on your system, execute the command below.
which bash
In our case, it’s /usr/bin/bash
Writing our first shell script
Now that you understand Shell scripts, the different Linux Shells available, and the Shebang line, let’s write our first Bash script.
There are two main methods that you can use to write and run Bash scripts.
- The Terminal (Recommended)
- The Graphical User Interface (GUI)
1. Write and run Bash scripts from Terminal
Below is a script that prints the name “hello world,” current time, and the hostname of our system. In our case, we used the nano editor to write the script. Execute the command below.
nano script_one.sh
Copy and paste the lines of code below. If you have a good understanding of Bash, you can add your lines of code. When done, save the file (Ctrl + S) and exit (Ctrl + X).
#! /usr/bin/bash echo "Hello World!" echo echo "Hostname is set to : $HOSTNAME" now=$(date +"%r") echo "Current time : $now" echo
Of course, this is a simple script, but it’s enough for us to understand how to run Bash scripts on Linux systems.
To get started, make the script executable by executing the chmod command using the syntax below.
chmod +x [script-name] e.g chmod +x script_one.sh
An additional exciting feature to note with Bash scripts is that they will have a different color from other scripts and files if the script is executable. See the image below.
To run our script from the Terminal, use the syntax below.
./[script-name] e.g. ./script_one.sh
That’s it! You have successfully run your first Bash script from the Terminal. Now let’s look at how you can create and run a script from the Graphical User Interface (GUI).
2. Create and run Bash scripts from the GUI
If you prefer working from the GUI, follow the steps below. Unfortunately, you will still have to interact with the Terminal at one point or another.
Launch your favorite code editor (Gedit, mousepad, etc.) and write your script. You can paste the code below for a test.
#! /usr/bin/bash echo "Hello World!" echo echo "Hostname is set to : $HOSTNAME" now=$(date +"%r") echo "Current time : $now" echo
Save the file and remember to add the ‘.sh’ extension. Right-click on the bash file and select properties to make the script executable. Select the ‘Permissions’ tab and tick the checkbox next to the “Allow executing file as a program” option. See the image below.
Now, when you double-click on the script file, you will see an option to run the file on the Terminal. Select “Run in Terminal,” and the script will execute on the Terminal.
That’s it! You have successfully created and run a script from your graphical user interface.
Conclusion
This post has given you a step-by-step guide on running a shell script on Linux. We have looked at both Terminal and GUI methods. However, we highly recommend running the script from the Terminal, which is much more versatile and will also help you get much more familiar with working with remote systems which only give you command-line access. If you are only getting started with Bash scripting, the posts below might come in quite handy.
- What is Bash shell, and know its importance in Linux
- Bash Scripting Tutorial for Beginners
- 35 Bash Script Examples