Linux command
continue 命令
文本
涉及管道、覆盖或删除,执行前请先确认路径和参数。
常用示例
Skip to the next iteration
for i in 1 2 3; do if [ "$i" -eq 2 ]; then continue; fi; echo "$i"; done
Skip iteration based on condition
while read line; do [[ "$line" == "#"* ]] && continue; echo "$line"; done < [file.txt]
Continue from an outer loop
for i in 1 2; do for j in a b; do [ "$j" = "a" ] && continue 2; echo "$i$j"; done; done
Skip processing empty lines
for file in *; do [ -z "$file" ] && continue; process "$file"; done
说明
continue is a shell builtin command that skips the remaining commands in the current iteration of an enclosing for, while, until, or select loop, and continues with the next iteration of that loop. When called without an argument, continue affects the innermost enclosing loop. When given a numeric argument n, it continues from the nth enclosing loop, counting outward from the innermost. This allows breaking out of multiple nested loops. The command is essential for controlling loop flow, particularly when certain conditions require skipping processing without terminating the entire loop. Unlike break, which exits the loop entirely, continue simply moves to the next iteration.
参数
- n
- Number of enclosing loops to skip out of. Default is 1 (innermost loop). Must be a positive integer.
FAQ
What is the continue command used for?
continue is a shell builtin command that skips the remaining commands in the current iteration of an enclosing for, while, until, or select loop, and continues with the next iteration of that loop. When called without an argument, continue affects the innermost enclosing loop. When given a numeric argument n, it continues from the nth enclosing loop, counting outward from the innermost. This allows breaking out of multiple nested loops. The command is essential for controlling loop flow, particularly when certain conditions require skipping processing without terminating the entire loop. Unlike break, which exits the loop entirely, continue simply moves to the next iteration.
How do I run a basic continue example?
Run `for i in 1 2 3; do if [ "$i" -eq 2 ]; then continue; fi; echo "$i"; done` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does n do in continue?
Number of enclosing loops to skip out of. Default is 1 (innermost loop). Must be a positive integer.