Bash Scripting Loops

LinuxLinuxBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, you will learn how to use loops in Bash scripting. Loops allow you to repeat a set of instructions multiple times, making your scripts more efficient and flexible. We will cover three types of loops: for, while, and until. Additionally, we will explore the break and continue statements, which allow you to control the loop execution.

The for Loop

The for loop is used to iterate over a list of values or the output of a command. Here is the basic syntax:

for item in [list]
do
    command(s)...
done

Create a file called ~/project/for.sh.

To loop through an array, you can use the following example:

NAMES=("Joe" "Jenny" "Sara" "Tony")
for name in "${NAMES[@]}"; do
  echo "My name is $name"
done
cd ~/project
chmod +x for.sh
./for.sh
My name is Joe
My name is Jenny
My name is Sara
My name is Tony

To loop through the output of a command, you can use the following example:

## create some txt files
touch file1.txt file2.txt file3.txt

## loop through the output of ls
for file in $(ls *.txt); do
  echo "File is: $file"
done
File is: file1.txt
File is: file2.txt
File is: file3.txt

The while Loop

The while loop is used to execute a block of code as long as a certain condition is true. Here is the basic syntax:

while [ condition ]
do
    command(s)...
done

Here is an example that counts down from 4:

count=4
while [ $count -gt 0 ]; do
  echo "Value of count is: $count"
  count=$(($count - 1))
done

Create a file called ~/project/while.sh.

cd ~/project
chmod +x while.sh
./while.sh
Value of count is: 4
Value of count is: 3
Value of count is: 2
Value of count is: 1

The until Loop

The until loop is the opposite of the while loop. It continues executing a block of code until a certain condition becomes true. Here is the basic syntax:

until [ condition ]
do
    command(s)...
done

Here is an example that counts up to 5:

count=1
until [ $count -gt 5 ]; do
  echo "Value of count is: $count"
  count=$(($count + 1))
done

Revise the file ~/project/while.sh to use the until loop.

Value of count is: 1
Value of count is: 2
Value of count is: 3
Value of count is: 4
Value of count is: 5

The break and continue Statements

The break and continue statements can be used to control the execution of a loop. break is used to exit the loop completely, while continue skips the rest of the current iteration and moves to the next one.

Here is an example that prints numbers from 0 to 4:

count=0
while [ $count -ge 0 ]; do
  echo "Value of count is: $count"
  count=$((count + 1))
  if [ $count -ge 5 ]; then
    break
  fi
done

Here is an example that prints only odd numbers from 1 to 10:

count=0
while [ $count -lt 10 ]; do
  count=$((count + 1))
  if [ $(($count % 2)) = 0 ]; then
    continue
  fi
  echo $count
done

Revise the file ~/project/while.sh to use the break and continue statements.

Value of count is: 1
Value of count is: 3
Value of count is: 5
Value of count is: 7
Value of count is: 9

Summary

In this lab, you learned how to use loops in Bash scripting. The for, while, and until loops allow you to repeat a block of code multiple times. You also learned about the break and continue statements, which help you control the loop execution. With this knowledge, you can write more efficient and flexible Bash scripts.

Other Linux Tutorials you may like