What Are Loops in Shell Scripting?

//

Angela Bailey

What Are Loops in Shell Scripting?

Loops are an essential construct in shell scripting that allows you to repeat a set of commands or operations multiple times. They provide a convenient way to automate repetitive tasks and iterate over a list of items. In this article, we will explore two types of loops commonly used in shell scripting: for loops and while loops.

The for Loop

The for loop is used when you know the number of iterations or when you want to iterate over a predefined list of values. It follows the following syntax:

for variable in list
do
    # commands to be executed
done

The variable represents the current value from the list, and the list is an array or a series of values separated by spaces. Inside the loop, you can perform any desired action using the current value.

To illustrate this further, let’s consider an example:

#!/bin/bash

fruits=("apple" "banana" "orange" "grape")

for fruit in "${fruits[@]}"
do
    echo "I like $fruit"
done

Output:
I like apple
I like banana
I like orange
I like grape

In this example, we have defined an array called “fruits”, and we use a for loop to iterate over each element. The variable “fruit” takes on each value from the array, and inside the loop, we print a sentence using that value.

The while Loop

The while loop is used when you want to repeat a set of commands until a specific condition becomes false. It follows this syntax:

while condition
do
    # commands to be executed
done

The condition is an expression that evaluates to either true or false. As long as the condition remains true, the loop will continue executing the commands inside it.

Let’s see an example:

count=1

while [ $count -le 5 ]
do
echo “Count: $count”
((count++))
done

Output:
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5

In this example, we initialize a variable “count” with a value of 1. The while loop checks if “count” is less than or equal to 5.

If it is, it prints the current count and increments the value of “count”. This process continues until the condition becomes false.

The break Statement

In both for and while loops, you can use the break statement to exit the loop prematurely. This can be useful when you want to stop iterating based on a particular condition.

Consider the following example:

for fruit in “${fruits[@]}”
do
if [ “$fruit” == “orange” ]
then
break
fi

echo “I like $fruit”
done

Output:
I like apple
I like banana

In this example, the loop iterates over the array of fruits. However, when it encounters the value “orange”, the break statement is executed, and the loop terminates immediately. As a result, only “apple” and “banana” are printed.

Conclusion

Loops are a fundamental concept in shell scripting that can greatly enhance its power and flexibility. The for loop allows you to iterate over a list of values, while the while loop helps you repeat commands until a condition becomes false. Understanding how to use loops effectively will enable you to write more efficient and automated shell scripts.

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy