2024-07-07
continue
’s FunctionalityThe primary purpose of continue
is to bypass remaining code within a loop’s body for a specific iteration. This is particularly useful when you encounter a condition where processing should be halted for that iteration only, without affecting the overall loop execution.
continue
in for
LoopsLet’s illustrate continue
’s use within a for
loop. This example iterates through numbers 1 to 10, skipping even numbers:
#!/bin/bash
for i in {1..10}; do
if (( i % 2 == 0 )); then
echo "Skipping even number: $i"
continue
fi
echo "Processing: $i"
done
This script’s output demonstrates how continue
prevents the “Processing: $i” line from executing for even numbers. The if
statement checks for even numbers using the modulo operator (%
). If the remainder is 0, continue
is executed, jumping to the next iteration.
continue
in while
LoopsThe continue
command works equally well in while
loops. Consider this example that reads lines from a file, skipping lines starting with ‘#’:
#!/bin/bash
while IFS= read -r line; do
if [[ "$line" == \#* ]]; then
echo "Skipping comment line: $line"
continue
fi
echo "Processing line: $line"
done < myfile.txt
This script reads myfile.txt
line by line. The if
statement checks if a line begins with #
. If it does, continue
skips the processing of that line, moving to the next.
continue
The power of continue
truly shines when dealing with nested loops. Here, we’ll show how to use it to control the inner loop based on a condition in the outer loop:
#!/bin/bash
for i in {1..3}; do
for j in {1..3}; do
if (( j == 2 )); then
echo "Skipping j = 2 in outer loop iteration $i"
continue 2 # continue to the next iteration of the outer loop
fi
echo "Processing i = $i, j = $j"
done
done
Notice the continue 2
. The number 2
specifies that the continue
command should jump to the next iteration of the second enclosing loop (the outer loop in this case). Without the 2
, it would only skip to the next iteration of the inner loop.
continue
and Error Handlingcontinue
can be strategically integrated with error handling to gracefully manage situations within loops. If an error occurs during a specific iteration, you can use continue
to prevent the error from halting the entire process. For example, you could skip processing a file if it’s inaccessible, without stopping the entire loop that processes a list of files.
The continue
statement is useful in various scenarios:
This illustrates the versatility and utility of the continue
command. By mastering its usage, you can write more efficient shell scripts.