Skip to main content

RHCSA Certification Course

Chapter #19: How to Create Simple Shell Scripts in Linux

In this chapter, you’ll learn how to create and run simple shell scripts in Linux, including conditionals, loops, and positional parameters.

Creating shell scripts is one of the most essential skills that Linux users should have at the tip of their fingers. Shell scripts play an enormous role in automating repetitive tasks which otherwise would be tedious executing line by line.

In this chapter, we highlight some of the basic shell scripting operations that every Linux user should have.

Introduction to Bash Scripting – Let’s Get Started
In this easy, step-by-step Bash series, we’ll guide you through everything you need to know about Bash scripting.

Create a Simple Shell Script

A shell script is a file that comprises ASCII text. We will start by creating a simple shell script, and to do this, we will use a text editor. There are quite a number of text editors, both command-line and GUI-based. For this guide, we will use the vim editor.

We will start off by creating a simple script that displays “Hello world” when executed.

vim hello.sh

Paste the following content in the file and save.

#!/bin/bash
# Print Hello world message
echo "Hello World!"

Let’s go over the shell script line by line.

  • The first line – #!/bin/bash - is known as the shebang header. This is a special construct that indicates what program will be used to interpret the script. In this case, this will be the bash shell indicated by /bin/bash. There are other scripting languages such as Python which is denoted by #!/usr/bin/python3 and Perl whose shebang header is denoted by #!/usr/bin/perl.
  • The second line is a comment. A comment is a statement that describes what a shell script does and is not executed when the script is run. Comments are always preceded by the hash sign #.
  • The last line is the command that prints the ‘Hello World’ message on the terminal.
Tip: Every shell script must have execution permission before it can run as a standalone program.

The next step is to make the script executable by assigning execute permission using the chmod command as shown.

chmod +x hello.sh

Finally, run the shell script using either of the commands:

bash hello.sh
OR
./hello.sh
Figure1: Create Hello World Shell Script

Using Conditional Statements to Execute Code

Like other programming languages, conditional statements are used in bash scripting to make decisions, with only a slight variation in the syntax. We are going to cover the if, if-else, and elif conditional statements.

Example of an if Statement Only

The if statement can be used to test single or multiple conditions. We will start off with the fundamental use of the if statement to test a single condition. The if statement is defined by the if ... fi blocks.

if command
then
  statement
fi

Note: The fi keyword is simply if spelled backward and marks the end of the if-block.

Let’s take a look at the shell script below.

#!/bin/bash
echo 'Enter the score'
read x

if [[ $x == 70 ]]; then
  echo 'Good job!'
fi

The above shell script prompts the user to provide a score that is then stored in a variable x. If the score corresponds to 70, the script returns the output “Good job!”. The comparison operator == is used to test if the score entered, which is stored in the variable x, is equivalent to 70.

Figure2: if Statement in Shell Script

Other comparison operators you can use include:

  • -eq - Equal to
  • -ne - Not equal to
  • -lt - Less than
  • -le - Less than or equal to
  • -gt - Greater than
  • -ge - Greater than or equal to
🚫 Correction: -lt was listed twice; the second one should be -gt (greater than).

For example, the if-statement block below prints out ‘Work Harder’ if the input score is any value less than 50.

if [[ $x -lt 50 ]]; then
  echo 'Work Harder!'
fi
Figure3: if Statement in Shell Script

Example of an if-else Statement

For situations where you have 2 possible outcomes: - whether this or that - the if-else statement comes in handy.

if command
then
  statement1
else
  statement2
fi

The script below reads the input score and checks whether it is greater than or equal to 70.

If the score is greater than or equal to 70, you get a ‘Great job, You passed!’ message. However, if the score falls below 70, the output ‘You failed’ will be printed.

#!/bin/bash

echo 'Enter the score'

read x

if [[ $x -ge 70 ]]; then
  echo 'Great job, You passed!'
else
  echo  'You failed'
fi
Figure4: if-else statement in Shell Script