Bash Continue How to Resume a Loop

Bash is a popular command-line interface shell used extensively in Linux and Unix operating systems. One of its most useful features is the ability to execute loops, which automate repetitive tasks. Sometimes you may need to skip certain iterations within a loop or control its flow. This is where the Bash continue statement becomes essential.

What is a Bash Loop?

A loop is a programming construct that executes a block of code repeatedly. Bash supports several types of loops including for loops (which iterate over a set of values) and while loops (which execute as long as a condition remains true).

The Bash Continue Statement

The continue statement skips the current iteration of a loop and moves directly to the next iteration. This is useful when you need to bypass certain values or conditions. The syntax is simple:

continue

When Bash encounters a continue statement, it immediately stops executing the current iteration and jumps to the next one.

Using Continue with For Loop

Here's how to use continue with a for loop to skip even numbers:

Example

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

This example uses the range operator {1..10} to iterate through values 1 to 10. The modulus operator checks if the current value is divisible by 2. If it is, continue skips that iteration.

Output

1
3
5
7
9

The even numbers (2, 4, 6, 8, 10) were skipped because they are divisible by 2.

Using Continue with While Loop

Here's an example using continue with a while loop to skip values greater than 5:

Example

i=1
while (( i  5 )); then
        (( i++ ))
        continue
    fi
    echo $i
    (( i++ ))
done

This loop increments i and uses continue to skip printing values greater than 5. Note the important placement of (( i++ )) before continue to avoid infinite loops.

Output

1
2
3
4
5

Resuming Interrupted Loops

Sometimes you need to interrupt a loop with break and resume it later. Here are two approaches:

Using Loop Control Variables

i=1
while (( i 

This approach uses a variable to track the loop's state, allowing you to resume from where you left off.

Output

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

Using Functions

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

This approach encapsulates the loop logic in a function and uses the return value to determine where to resume.

Output

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

Key Points

  • continue skips the current iteration and moves to the next one

  • Always ensure loop counters are properly incremented when using continue in while loops

  • Use break to exit loops entirely, then resume with control variables or functions

  • Functions can return values to indicate where loops were interrupted

Conclusion

The Bash continue statement is essential for controlling loop flow and skipping specific iterations. Combined with loop control variables or functions, it enables sophisticated loop management for automation tasks and data processing in shell scripting.

Updated on: 2026-03-17T09:01:38+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements