102-500 practice question 10 of 30

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

Full set: 233 questions
Answer options
A: for Correct answer
B: loop
C: until
D: while
Correct Answer:
A. for
Explanation

The code sample shows the syntax for a for loop in a shell script. The for loop is used to iterate over a list of items and perform a set of commands for each item. The general syntax is for name [in words ...]; do commands; done. In the provided example, i is the variable, and .txt is a glob pattern that expands to a list of all files in the current directory ending with .txt. The for keyword is required to initiate this type of loop structure.

Why Incorrect

B. loop is not a standard keyword for creating loops in bash or other POSIX-compliant shells.
C. The until loop executes commands as long as its test condition is false and uses the syntax until [condition]; do ...; done.
D. The while loop executes commands as long as its test condition is true and uses the syntax while [condition]; do ...; done.

References

1. GNU Bash Reference Manual, Version 5.2, "3.2.4.1 The for Loop". The manual provides the syntax: for name [ [in [word ...] ] ; ] do commands; done. The example for i in .txt directly matches this pattern.

2. The Open Group Base Specifications Issue 7, 2018 edition, IEEE Std 1003.1-2017, "Shell & Utilities", Chapter 2, "Shell Command Language", Section 2.9.4 "For Loop". This standard defines the for loop with the exact syntax shown in the question.

3. Stanford University, CS107: Computer Organization & Systems, "Unix and C", Lecture 02, "Shell". Course materials cover fundamental shell scripting constructs, including the syntax and usage of the for loop for iterating over file lists.

Scroll to Top