nerdexam
LPI

102-500 · Question #194

What keyword is missing from this code sample of a shell script? ____ i in *.txt; do echo $i done

The correct answer is A. for. Option A (for) is correct because the construct for i in *.txt; do ... done is the standard Bash for loop syntax, which iterates over a list of items - in this case, all .txt files - assigning each to the variable i on each pass. Why the distractors are wrong: B. loop - not a…

Shells and Shell Scripting

Question

What keyword is missing from this code sample of a shell script? ____ i in *.txt; do echo $i done

Options

  • Afor
  • Bloop
  • Cuntil
  • Dwhile

How the community answered

(24 responses)
  • A
    75% (18)
  • B
    8% (2)
  • C
    4% (1)
  • D
    13% (3)

Explanation

Option A (for) is correct because the construct for i in *.txt; do ... done is the standard Bash for loop syntax, which iterates over a list of items - in this case, all .txt files - assigning each to the variable i on each pass.

Why the distractors are wrong:

  • B. loop - not a keyword in Bash or POSIX shell at all; it simply doesn't exist.
  • C. until - is a real shell keyword, but it pairs with a condition expression (until [condition]; do ... done), not an in clause iterating over a list.
  • D. while - also real, but like until, it expects a condition (while [condition]; do ... done), not the in list pattern seen here.

Memory tip: The giveaway is the word in - only for uses the for VAR in LIST pattern. If you see in between the variable and the list, it's always for.

Topics

#for loops#shell control flow#loop syntax#bash scripting

Community Discussion

No community discussion yet for this question.

Full 102-500 Practice