How do I continue in a for or while loop in Bash under UNIX or Linux operating systems?
The continue statement is used to resume the next iteration of the enclosing FOR, WHILE or UNTIL loop.
Bash for Loop continue Syntax
for i in something do [ condition ] && continue cmd1 cmd2 done
A sample shell script to print number from 1 to 6 but skip printing number 3 and 6 using a for loop:
#!/bin/bash for i in 1 2 3 4 5 6 do ### just skip printing $i; if it is 3 or 6 ### if [ $i -eq 3 -o $i -eq 6 ] then continue ### resumes iteration of an enclosing for loop ### fi # print $i echo "$i" done
Bash while Loop continue Syntax
while true do [ condition1 ] && continue cmd1 cmd2 done
A sample shell script to print number from 1 to 6 but skip printing number 3 and 6 using a while loop:
#!/bin/bash i=0 while [ $i -lt 6 ] do (( i++ )) ### resumes iteration of an enclosing while loop if $i is 3 or 6 ### [ $i -eq 3 -o $i -eq 6 ] && continue echo "$i" done
See also:
- Continue statement from the Linux shell scripting wiki
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop













{ 0 comments… add one now }