1. Overview

In Linux, a shell script helps boost efficiency and productivity by allowing us to automate repetitive tasks. Additionally, it enables us to streamline complex workflows and ensure consistent execution of commands. As a result, we can perform tasks more effectively.

In this tutorial, we’ll talk about shell scripts in Linux.

2. An Overview of Shell Scripts

In this section, let’s dive into learning about the shell itself, shell scripts, and how to execute them.

2.1. Shell

System administrators can utilize the shell as a user interface to access the services provided by the operating system. The shell not only interprets the commands entered by the administrator, but also executes them.

Additionally, Bash (Bourne Again Shell), sh (Bourne Shell), csh (C Shell), ksh (Korn Shell), and zsh (Z Shell) are examples of common shells in Linux.

2.2. Shell Script

A shell script is a file containing a sequence of commands for the shell to execute. Typically, it utilizes a .sh extension.

Using shell scripts has various benefits, such as:

  • automation – automates repetitive tasks
  • efficiency – improves the workflow efficiency by executing multiple commands
  • customization – allows administrators to customize commands for specific operations
  • portability – shell scripts can execute on any system with a compatible shell
  • development – using automated scripts to build, test, and deploy applications

At this point, let’s discuss how to execute the shell script.

2.3. Executing a Shell Script

First, we can specify the interpreter to run the script:

$ bash example.sh

In the command above, bash declares that the Bash shell should run the script.

Alternatively, we can make the script executable before we execute it:

$ chmod +x example.sh

Here, we utilize the chmod command to add the executable permission to the file for the owner, group, and others. Thereafter, we can specify the script’s path to run it:

$ ./example.sh

Above, we successfully execute the script.

3. Concepts in Shell Scripts

A shell script uses similar commands to the ones in the shell command line.

3.1. Shebang

The file starts with a shebang (!#) which plays a critical role in shell scripting, especially when dealing with different shells:

#!/bin/bash

The shebang (!#) specifies the path to the interpreter that should execute this file, while /bin/bash represents the path to the Bash shell interpreter.

3.2. Comments

During execution, the shell discards comments. To clarify, a comment starts with the # symbol and we use them to explain the instructions in the script to other readers and maintainers. Thus, comments make shell scripts easy to manage and understand.

3.3. Variables

Moreover, we need variables to store data that can be accessible throughout the script:

#!/bin/bash
NAME="Francis"

Here, we define a variable named NAME and assign it the value Francis.

3.4. Control Structures

In the case of control structures, let’s discuss conditional statements (if, else) and loops. These control structures help administrators control the order in which the script commands are executed and also perform repetitive tasks.

First, we’ll explore conditionals:

#!/bin/bash
NAME="Francis"
if [ "$NAME" == "Francis" ]; then
  echo "Variable is Francis"
else
  echo "Variable is not Francis"
fi

This is the breakdown:

  • if [ “$NAME” == “Francis” ]; then – the if statement checks whether the value of NAME is Francis
  • echo “Value of NAME is Francis” – if the condition is true the echo command prints Value of NAME is Francis in the terminal
  • else – if the condition is false, the else statement executes and prints Value of NAME is not Francis in the terminal
  • fi – specifies the end of the if statement

Next, let’s look at working with loops:

#!/bin/bash
for i in {1..4}
do
   echo "Number: $i"
done

Let’s break down the script contents above:

  • for i in {1..4} – this line initiates a for loop that iterates over the sequence of numbers from 1 to 4 while storing each number in the variable i one at a time
  • do – marks the start of the loop body
  • echo “Number: $i” – this command exists within the loop and prints the current value of i prefixed with “Number:”
  • done – declares the end of the loop body

What’s more, in the case of loops, we can choose to utilize a while loop instead:

#!/bin/bash
i=1
while [ $i -le 4 ]
do
  echo "Number: $i"
  ((i++))
done

Let’s analyze the commands:

  • i=1 – this line initializes the variable counter to 1
  • while [ $i -le 4 ] – this command ensures that the while loop continues to execute as long as the value of i is equal to or less than 4 as defined in the condition [ $i -le 4 ]
  • echo “Number: $i” – this command occurs inside the loop to display the current value of i prefixed with “Number:”
  • ((i++)) – raises the value of i by 1

Here, we examine for and while loops.

3.5. Functions

Another important aspect is the use of functions that allow us to group a set of commands into a single logical unit:

#!/bin/bash
function greet() {
  echo "Hello $1"
}

greet "Francis"

So, let’s explore these instructions:

  • function greet() {…} – declares the greet function
  • echo “Hello $1” – prints Hello
  • greet “Francis” – this line calls the greet function with the argument “Francis” which is passed to the function and stored in $1

Functions are fundamental in shell scripting.

3.6. Input and Output

In shell scripts, we can utilize input and output operations to interact with a user:

#!/bin/bash

echo "Enter your name:"
read NAME
echo "Hello, $NAME"

This script asks the user to input their name before greeting them. To clarify, the read command captures the user’s input and stores it in the NAME variable.

4. Conclusion

In this article, we learned what a shell script is and why they’re useful.

Shell scripts are important for both beginners and advanced Linux users since they help us automate tasks and achieve better control over the system’s operations.