nerdexam
CompTIA

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.

Shells, Scripting and Data Management

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

(28 responses)
  • A
    89% (25)
  • C
    7% (2)
  • D
    4% (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.

AforCorrect

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`.

Bloop

`loop` is not a valid keyword for initiating a loop in bash scripting; it's a generic term.

Cuntil

`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`.

Dwhile

`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

#shell scripting#for loop#bash

Community Discussion

No community discussion yet for this question.

Full LX0-104 Practice