Exiting from the current loop iteration with the continue command

Using the continue command, it is possible to exit from the current iteration of the loop and resume the next iteration of the loop. We use the for, while, or until commands for loop iterations.

The following is the for_09.sh script for the loop with the continue command to skip a certain part of the loop commands:

#!/bin/bash 
for x in 1 2 3 
do 
    echo before $x 
    continue 1 
    echo after $x 
done 
exit 0 

Let's test the program:

$ chmod +x for_09.sh$ ./for_09.sh

The following will be the output after executing the preceding commands:

    before 1
    before 2
    before 3
  

The following is the for_10.sh script in which we will check all files and directories. If the file is found, we will print ...

Get Learning Linux Shell Scripting - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.