LX0-104 · Question #534
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. The for keyword initiates a for loop in shell scripting, which iterates over a list of items, such as file names matching a wildcard pattern.
Question
Options
- Afor
- Bloop
- Cuntil
- Dwhile
How the community answered
(28 responses)- A89% (25)
- C7% (2)
- D4% (1)
Why each option
The `for` keyword initiates a `for` loop in shell scripting, which iterates over a list of items, such as file names matching a wildcard pattern.
The structure `for variable in list; do commands; done` is the standard syntax for a `for` loop in bash and other shells. In this example, `for i in *.txt` means the loop will iterate over each file ending with `.txt` in the current directory, assigning each filename to the variable `i`.
`loop` is not a valid keyword for initiating a loop in bash scripting; it's a generic term.
`until` is used for `until` loops, which execute commands repeatedly as long as a command's exit status is non-zero (false), following the syntax `until condition; do commands; done`.
`while` is used for `while` loops, which execute commands repeatedly as long as a command's exit status is zero (true), following the syntax `while condition; do commands; done`.
Concept tested: Bash for loop syntax
Source: https://www.gnu.org/software/bash/manual/bash.html#Looping-Constructs
Topics
Community Discussion
No community discussion yet for this question.