In Bash, you can use the continue
statement to skip the current iteration of a loop and move on to the next iteration. This is useful if you want to skip certain items in a loop that meet a particular condition, for example.
Here’s an example of using continue
in a for
loop:
for i in {1..10}; do
if [ $i -eq 5 ]; then
continue
fi
echo $i
done
In this example, the loop iterates over the numbers 1 to 10, but skips the number 5 using the continue
statement. The output of this loop will be:
1
2
3
4
6
7
8
9
10
Similarly, here’s an example of using continue
in a while
loop:
i=0
while [ $i -lt 10 ]; do
i=$((i+1))
if [ $i -eq 5 ]; then
continue
fi
echo $i
done
In this example, the loop increments a counter from 1 to 10, but skips the number 5 using the continue
statement. The output of this loop will be:
1
2
3
4
6
7
8
9
10