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…
Question
Options
- Afor
- Bloop
- Cuntil
- Dwhile
How the community answered
(24 responses)- A75% (18)
- B8% (2)
- C4% (1)
- D13% (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 aninclause iterating over a list. - D.
while- also real, but likeuntil, it expects a condition (while [condition]; do ... done), not thein listpattern 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
Community Discussion
No community discussion yet for this question.