Continue Loop Command

Bash Scripting Continue Examples

Continue command


Continue is a command that is used to allow you continue on to the next iteration of a loop. The continue command can be used within a "for loop", "while loop" and an "until loop"

For loop example with continue command:




john@john-desktop:~/scripts$ cat cont1.sh 
#!/bin/bash
#
for i in 1 2 3 4 5 6 7 8 9
do
     if [ $i -eq 5 ]; then
        printf "\n Skipping past 5, Continuing to next iteration\n"
        continue
     fi
  printf "\n Variable i has a value of $i\n"
done

In the above example the loop has skipped past the value of "5" and continued to the next iteration. This can be clearly seen in the output below:

Output from the above script



john@john-desktop:~/scripts$ ./cont1.sh 

 Variable i has a value of 1

 Variable i has a value of 2

 Variable i has a value of 3

 Variable i has a value of 4

 Skipping past 5, Continuing to next iteration

 Variable i has a value of 6

 Variable i has a value of 7

 Variable i has a value of 8

 Variable i has a value of 9