010-160 · Question #58
What keyword is missing from the following segment of the shell script? for i in *; cat $i done
The correct answer is A. do. A bash for loop requires the do keyword between the in clause and the loop body commands.
Question
Options
- Ado
- Bthen
- Cenod
- Dfi
- Erun
How the community answered
(35 responses)- A94% (33)
- B3% (1)
- E3% (1)
Why each option
A bash for loop requires the do keyword between the in clause and the loop body commands.
The correct POSIX/bash for loop syntax is: for i in *; do <commands>; done. The do keyword is mandatory and marks the beginning of the loop body; its absence causes an immediate syntax error from the shell.
then is used to begin the body of an if statement, not a for loop.
enod is not a valid keyword in any POSIX shell or bash; it does not exist in standard shell syntax.
fi closes an if-then-fi block and plays no role in for loop structure.
run is not a shell control-flow keyword and is not part of any standard shell loop construct.
Concept tested: Bash for loop syntax and required keywords
Source: https://www.gnu.org/software/bash/manual/bash.html#Looping-Constructs
Topics
Community Discussion
No community discussion yet for this question.