Bash Continue How to Resume a Loop


Bash is a popular command-line interface shell that is used extensively in Linux and Unix operating systems. One of most useful features of Bash is its ability to execute loops, which allow you to automate repetitive tasks. However, sometimes you may want to interrupt a loop and then continue it later. This is where Bash continue statement comes in handy. In this article, we will explore how to use Bash continue statement to resume a loop.

What is a Bash Loop?

Before we dive into details of Bash continue statement, it's important to understand what a Bash loop is. A loop is a programming construct that allows you to execute a block of code repeatedly. In Bash, there are two types of loops: for loop and while loop. for loop is used to iterate over a set of values, while while loop is used to execute a block of code repeatedly as long as a certain condition is true.

The Bash Continue Statement

The Bash continue statement is used to skip current iteration of a loop and move on to next iteration. This is useful if you want to skip over certain values or if you need to interrupt a loop and resume it later. syntax for continue statement is as follows −

continue

When Bash encounters continue statement, it immediately stops executing current iteration of loop and moves on to next iteration.

Using Bash Continue with for Loop

Let's take a look at how to use Bash continue statement with for loop. In this example, we will iterate over a set of values and skip over any values that are divisible by 2 −

Example

for i in {1..10}
do
   if (( i % 2 == 0 )); then
      continue
   fi
      echo $i
done

In this example, we are using {1..10} range operator to iterate over values 1 through 10. Inside loop, we use modulus operator to check if current value is divisible by 2. If it is, we use continue statement to skip over current iteration and move on to next one. If it's not divisible by 2, we use echo command to print current value.

Output

The output of this script will be −

1
3
5
7
9

As you can see, values 2, 4, 6, 8, and 10 were skipped over because they are divisible by 2.

Using Bash Continue with while Loop

Now let's take a look at how to use Bash continue statement with while loop. In this example, we will use a while loop to iterate over values 1 through 10 and skip over any values that are greater than 5 −

Example

i=1
while (( i <= 10 ))
do
    if (( i > 5 )); then
        (( i++ ))
        continue
    fi
    echo $i
    (( i++ ))
done

In this example, we initialize variable i to 1 and use a while loop to iterate over values 1 through 10. Inside loop, we check if current value is greater than 5. If it is, we use continue statement to skip over current iteration and move on to next one. We also use (( i++ )) command to increment value of i. If current value is not greater than 5, we use echo command to print current value.

Output

The output of this script will be −

1
2
3
4
5

As you can see, values 6, 7,

Continuing a Loop After Interrupting it

Now that we've seen how to use Bash continue statement to skip over iterations of a loop, let's take a look at how to resume a loop after interrupting it. There are a few ways to do this, depending on situation.

Using a Loop Control Variable

One way to resume a loop after interrupting it is to use a loop control variable. A loop control variable is a variable that you use to keep track of current state of loop.

Example

i=1
while (( i <= 10 ))
do
    if (( i == 5 )); then
        break
    fi
    echo $i
    (( i++ ))
done

echo "Loop interrupted at i = $i"

while (( i <= 10 ))
do
    echo $i
    (( i++ ))
done

In this example, we initialize variable i to 1 and use a while loop to iterate over values 1 through 10. Inside loop, we check if current value of i is equal to 5. If it is, we use break statement to interrupt loop. We then print a message indicating that loop was interrupted at i = 5.

After loop is interrupted, we use another while loop to resume iteration where it left off. We simply start loop with current value of i and continue iterating until we reach end of loop.

Output

The output of this script will be −

1
2
3
4
Loop interrupted at i = 5
5
6
7
8
9
10

Using a Function Another way to resume a loop after interrupting it is to use a function. A function is a block of code that can be called repeatedly. H

Example

function myloop() {
    for i in {1..10}
    do
        if (( i == 5 )); then
            return $i
        fi
        echo $i
    done
}

i=$(myloop)
echo "Loop interrupted at i = $i"

for (( j=$i; j<=10; j++ ))
do
    echo $j
done

In this example, we define a function called myloop that contains a for loop. Inside loop, we check if current value of i is equal to 5. If it is, we use return statement to interrupt loop and return value of i.

After loop is interrupted, we call myloop function and store return value in variable i. We then print a message indicating that loop was interrupted at i = 5.

Finally, we use a for loop to resume iteration where it left off. We initialize variable j to value of i and continue iterating until we reach end of loop.

Output

The output of this script will be −

1
2
3
4
Loop interrupted at i = 5
5
6
7
8
9
10

Conclusion

The Bash continue statement is a powerful tool that allows you to skip over iterations of a loop and resume loop where it left off. By using continue statement in conjunction with loop control variables or functions, you can interrupt and resume loops in a variety of different ways. Whether you're automating repetitive tasks or processing large amounts of data, Bash continue statement is an essential tool for any Bash programmer.

Updated on: 31-Mar-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements