Post

Loops in bash scripts | LinuxGist

This article will provide details on how we can use loops in bash scripts.

Introduction

In Bash scripting, loops allow you to automate repetitive tasks by iterating over a set of values or conditions.

Here are the common types of loops in Bash:

1. for loop

In Bash scripting, a for loop is used to iterate through a list of items (like numbers, filenames, or strings) and perform a specific action for each item. Below are some examples of how you can use for loops in Bash.

Basic Syntax

1
2
3
4
for variable in items_list
do
    # commands to be executed
done

Example 1: Loop Through a List of Numbers

1
2
3
4
for i in 1 2 3 4 5 6 7 8 9
do
    echo "Number is $i"
done

Output:

1
2
3
4
5
6
7
8
9
Number is 1
Number is 2
Number is 3
Number is 4
Number is 5
Number is 6
Number is 7
Number is 8
Number is 9

Example 2: Loop Through a Range of Numbers

1
2
3
4
for i in {1..9}
do
    echo "Number is $i"
done

Output:

1
2
3
4
5
6
7
8
9
Number is 1
Number is 2
Number is 3
Number is 4
Number is 5
Number is 6
Number is 7
Number is 8
Number is 9

Example 3: Loop Through a List of Filenames

1
2
3
4
for file in *.txt
do
    echo "Processing $file"
done

Output:

1
2
3
Processing foo1.txt
Processing foo2.txt
# Assuming there are two text files named foo1.txt and foo2.txt in current directory

Example 4: Loop Through a Command’s Output

1
2
3
4
for line in $(cat input.txt)
do
    echo "Line is $line"
done

Output:

1
2
3
Line is This is the first line.
Line is This is the second line.
# Assuming input.txt contains two lines as shown above

Example 5: Loop Through a String

1
2
3
4
5
string="Hello"
for (( i=0; i<${#string}; i++ ))
do
    echo "Character at position $i is ${string:$i:1}"
done

Output:

1
2
3
4
5
Character at position 0 is H
Character at position 1 is e
Character at position 2 is l
Character at position 3 is l
Character at position 4 is o

Example 6: Loop Through Multiple Commands

1
2
3
4
for cmd in ls pwd echo "Hello"
do
    $cmd
done

Output:

1
2
3
Desktop  Documents  Downloads  Music  Pictures
/home/user/foo
Hello

Example 7: Nested Loops

1
2
3
4
5
6
7
for i in {1..3}
do
    for j in {a..c}
    do
        echo "i is $i and j is $j"
    done
done

Output:

1
2
3
4
5
6
7
8
9
i is 1 and j is a
i is 1 and j is b
i is 1 and j is c
i is 2 and j is a
i is 2 and j is b
i is 2 and j is c
i is 3 and j is a
i is 3 and j is b
i is 3 and j is c

Example 8: Using read to Loop Through Input

1
2
3
while read line; do
    echo "Line is $line"
done <<< "Line1\nLine2\nLine3"

Output:

1
2
3
Line is Line1
Line is Line2
Line is Line3

2. while and until loop

A while loop in Bash is used to execute a set of commands as long as a specified condition remains true. Here’s how you can use a while loop:

Basic Syntax

1
2
3
4
5
6
while [ condition ]
do
    command1
    command2
    ...
done

Example 1: Simple While Loop

This example reads lines from a file until the end of the file is reached.

1
2
3
4
5
6
7
#!/bin/bash

# Open the file and read line by line
while IFS= read -r line
do
    echo "Line is $line"
done < input.txt

Example 2: Using Condition in While Loop

This example prints numbers from 1 to 5.

1
2
3
4
5
6
7
8
#!/bin/bash

count=1
while [ $count -le 5 ]
do
    echo "Number is $count"
    ((count++))
done

Example 3: Infinite Loop with Break Statement

This example reads user input until the user enters ‘q’ to quit.

1
2
3
4
5
6
7
8
9
#!/bin/bash

while true
do
    read -p "Enter a character (q to quit): " char
    if [ "$char" = "q" ]; then
        break
    fi
done

Example 4: Reading Input Using Until Loop

This example uses an until loop to prompt the user until they enter ‘y’.

1
2
3
4
5
6
7
#!/bin/bash

answer=""
until [ "$answer" = "y" ]
do
    read -p "Do you want to continue? (y/n): " answer
done

Example 5: Using Command Output in While Loop

This example checks if a file exists until it is created.

1
2
3
4
5
6
7
8
9
#!/bin/bash

file="newfile.txt"
while [ ! -f "$file" ]
do
    echo "File $file does not exist. Waiting..."
    sleep 1
done
echo "File $file has been created."

Example 6: Using Arithmetic Condition in While Loop

This example counts down from 10 to 1.

1
2
3
4
5
6
7
8
#!/bin/bash

count=10
while [ $count -ge 1 ]
do
    echo "Count is $count"
    ((count--))
done
  • While Loop: Executes as long as the condition is true.
  • Until Loop: Opposite of while loop, it executes until the condition is false.

Summary

These examples should give you a good understanding of how to use for and while/until loops in Bash. These snippets can be easily modified to suit your specific needs.

This post is licensed under CC BY 4.0 by the author.