nerdexam
Exams010-160Questions#90
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; do and done are required.
  • D. This resembles find -exec syntax, not a for loop.
  • E. The foreach keyword 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.

Full 010-160 Practice