Automation with shell scripts

command-line development-tools devops linux

What is a shell?

Shell is a term to describe computer interfaces that we use to interact with our operating system. Shells can be graphical or text-based using a command line. We typically use a graphical user interface, a graphical shell, to control the computer. When we open a command line window called terminal, a text-based shell is started for us in that window. Using this shell, we can control the computer by issuing text-based commands.

We can use several command-line shells, but the most common and useful to know is Bourne Again SHell, known as Bash. This is a shell commonly associated with Linux. Still, it is also a shell that we can install on macOS and Windows, e.g., by using Ubuntu on Windows.

What are shell scripts?

When talking about shell scripts, we typically talk about writing scripts for the command line. A shell script is a type of executable text file containing instructions on what to do when the file is run. The file can be opened and edited in a text editor and run using the command line.

A shell script can be straightforward, only executing one command or grouping many commands with conditions, loops, and other control mechanisms. It is typically used as a set of instructions we want to run together without typing or remembering them.

We refer to the command-line environment as Bash but use the same name to refer to its command language. Therefore, we consider Bash a programming language and say that we program in Bash.

How can shell scripts help us?

Shell scripts usually help us automate tasks we want to do more than once. Some examples include:

Our first shell script

To create an executable file for Bash, we need to do two things:

So let’s create and open a blank text file and let’s start typing:

#!/bin/bash

The first two special characters together (# and !) at the beginning are called shebang. Following the shebang, we specify what program will execute our file. In the case of typical shell scripts, it will be a path to Bash in the operating system. Without this first line, Bash (or another shell) wouldn’t know that we want to run this file with Bash itself.

Let’s add a Bash command to demonstrate that our script has been executed. A good example is to use the command called echo, which will print its arguments to the console.

#!/bin/bash
echo "Output from first shell script"

Let’s save our first script as print.sh. We don’t have to specify the file extension. It doesn’t affect the ability of the file to be executed. Still, it serves to identify shell scripts among other files.

Before we run it, we need to make this text file executable. We will do it by opening a terminal and navigating to the folder where we created the file with command called cd (change directory) and then using a program called chmod:

cd /path/to/the/print.sh/directory/
chmod u+x print.sh

With u+x, we only allow this file to be executed by the owner of the file (which is us because we have created it). Still, in case we create a script that will have to run under another user, we can make the file executable by everyone with +x. Note that it is best to only allow permissions to the users or group that need it, so you might want to study the permission system more for more serious computing.

How to run a shell script?

Now we can easily run this file in the terminal (assuming we are still in the same directory in our open terminal):

./print.sh
Output from first shell script

We can see our string “Output from first shell script” printed to the console.

Congratulations! We have just created and executed a shell script!

Last updated on 6.7.2019.