bash: Looping Constructs
1
1 3.2.4.1 Looping Constructs
1 ..........................
1
1 Bash supports the following looping constructs.
1
1 Note that wherever a ';' appears in the description of a command's
1 syntax, it may be replaced with one or more newlines.
1
1 'until'
1 The syntax of the 'until' command is:
1
1 until TEST-COMMANDS; do CONSEQUENT-COMMANDS; done
1
1 Execute CONSEQUENT-COMMANDS as long as TEST-COMMANDS has an exit
1 status which is not zero. The return status is the exit status of
1 the last command executed in CONSEQUENT-COMMANDS, or zero if none
1 was executed.
1
1 'while'
1 The syntax of the 'while' command is:
1
1 while TEST-COMMANDS; do CONSEQUENT-COMMANDS; done
1
1 Execute CONSEQUENT-COMMANDS as long as TEST-COMMANDS has an exit
1 status of zero. The return status is the exit status of the last
1 command executed in CONSEQUENT-COMMANDS, or zero if none was
1 executed.
1
1 'for'
1 The syntax of the 'for' command is:
1
1 for NAME [ [in [WORDS ...] ] ; ] do COMMANDS; done
1
1 Expand WORDS, and execute COMMANDS once for each member in the
1 resultant list, with NAME bound to the current member. If 'in
1 WORDS' is not present, the 'for' command executes the COMMANDS once
1 for each positional parameter that is set, as if 'in "$@"' had been
1 specified (⇒Special Parameters). The return status is the
1 exit status of the last command that executes. If there are no
1 items in the expansion of WORDS, no commands are executed, and the
1 return status is zero.
1
1 An alternate form of the 'for' command is also supported:
1
1 for (( EXPR1 ; EXPR2 ; EXPR3 )) ; do COMMANDS ; done
1
1 First, the arithmetic expression EXPR1 is evaluated according to
1 the rules described below (⇒Shell Arithmetic). The
1 arithmetic expression EXPR2 is then evaluated repeatedly until it
1 evaluates to zero. Each time EXPR2 evaluates to a non-zero value,
1 COMMANDS are executed and the arithmetic expression EXPR3 is
1 evaluated. If any expression is omitted, it behaves as if it
1 evaluates to 1. The return value is the exit status of the last
1 command in COMMANDS that is executed, or false if any of the
1 expressions is invalid.
1
1 The 'break' and 'continue' builtins (⇒Bourne Shell Builtins)
1 may be used to control loop execution.
1