Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: March 19, 2025
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.
In this section, let’s dive into learning about the shell itself, shell scripts, and how to execute them.
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.
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:
At this point, let’s discuss how to execute the 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.
A shell script uses similar commands to the ones in the shell command line.
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.
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.
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.
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:
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:
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:
Here, we examine for and while loops.
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:
Functions are fundamental in shell scripting.
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.
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.