LPI
010-160 · Question #90
010-160 Question #90: Real Exam Question with Answer & Explanation
The correct answer is C: for file in *.txt do echo $i done. The correct Bash for loop uses for variable in list, followed by do, the loop body, and done. Only option C matches this valid shell syntax.
Question
Which of the following examples shows the general structure of a for loop in a shell script?
Options
- Afor * .txt == file => echo $file
- Bfor * .txt ( echo $i )
- Cfor file in *.txt do echo $i done
- Dfor ls *.txt exec {} ;
- Eforeach @(file) { echo $i }
Explanation
The correct Bash for loop uses for variable in list, followed by do, the loop body, and done. Only option C matches this valid shell syntax.
Common mistakes.
- A. The
=>arrow and==syntax are not valid Bash for loop constructs. - B. Parentheses are not used to delimit Bash for loop bodies;
doanddoneare required. - D. This resembles
find -execsyntax, not a for loop. - E. The
foreachkeyword with@()and{{}}braces is not valid Bash syntax.
Concept tested. Bash shell for loop syntax and structure
Reference. https://www.gnu.org/software/bash/manual/bash.html#Looping-Constructs
Community Discussion
No community discussion yet for this question.