Open In App

Continue Command in Linux with examples

Last Updated : 09 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

continue is a command which is used to skip the current iteration in for, while, and until loop. It is used in scripting languages and shell scripts to control the flow of executions. It takes one more parameter [N], if N is mentioned then it continues from the nth enclosing loop.

The syntax for the `continue` command in Linux

continue
or 
continue [N]

Usage and Examples of `Continue` Command

`continue` statement in for loop 

Our script code is mentioned below.

vim for_loop.sh

Creating a script name “for_loop.sh”

#!/bin/bash
for i in `seq 1 10`
do
   if (( $i==5))
       then
       continue
   fi
   echo $i
done

chmod +x for_loop.sh

Making our script executable

./for_loop.sh

Executing Script

./for_loop.sh

./for_loop.sh

As we can see that continue command has skipped the `echo $i` command when i =5 and then our loop continued with the remaining iterations and prints the number 1 to 4, and 6 to 10.

`continue` statement in the while loop 

Our script code is mentioned below.

vim while_loop.sh

Creating a script name “while_loop.sh”

#!/bin/bash
i=1
while(( $i<=10))
do
       ((++i))
       if(( $i==5))
       then
               continue
       fi
       echo $i
done
 

chmod +x while_loop.sh

Making our script executable

./while_loop.sh

Executing Script

./while_loop.sh

./while_loop.sh

As we can see the number 5 is not there in the output because the continue command prevents the script for reaching the `echo $i` command when `i` is equal to 5.

`continue` statement in until loop 

Our script code is mentioned below.

vim until_loop.sh

Creating a script name “until_loop.sh”

#!/bin/bash
i=0
until(( $i==10 ))
do
       ((++i))
       if(( $i==5 ))
       then
               continue
       fi
       echo $i
done
 

chmod +x until_loop.sh

Making our script executable

./until_loop.sh

Executing Script

./until_loop.sh

./until_loop.sh

As we can see number 5 is skipped in the output because of the `continue` command when `i` was equal to 5.

`–help` option in continue command in Linux

Displays help information.

continue --help
continue --help

continue –help

Conclusion

In this article, we have discussed the use of `continue` command which is used to skip the current iteration and jump to the next iteration. We have discussed the usage of `continue` command by taking three examples, `continue` command in while, for and until loop. With the understanding of this article a user can easily understand the working of continue command in Linux.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads