nerdexam
CompTIA

LX0-104 · Question #547

Which of the following outputs will the below command sequence produce? echo '1 2 3 4 5 6' | while read a b c; do echo result: $c $b $a; done

The correct answer is A. result: 3 4 5 6 2 1. The echo command pipes a single line of numbers to a while read loop where read a b c assigns the first two words to a and b, and all remaining words to c.

Shells, Scripting and Data Management

Question

Which of the following outputs will the below command sequence produce? echo '1 2 3 4 5 6' | while read a b c; do echo result: $c $b $a; done

Options

  • Aresult: 3 4 5 6 2 1
  • Bresult: 1 2 3 4 5 6
  • Cresult: 6 5 4
  • Dresult: 6 5 4 3 2 1
  • Eresult: 3 2 1

How the community answered

(57 responses)
  • A
    75% (43)
  • B
    14% (8)
  • C
    5% (3)
  • D
    4% (2)
  • E
    2% (1)

Why each option

The `echo` command pipes a single line of numbers to a `while read` loop where `read a b c` assigns the first two words to `a` and `b`, and all remaining words to `c`.

Aresult: 3 4 5 6 2 1Correct

The `read` command assigns "1" to `a`, "2" to `b`, and "3 4 5 6" (the rest of the line) to `c`. The `echo result: $c $b $a` command then prints these values in the order `c`, `b`, `a`, resulting in "result: 3 4 5 6 2 1".

Bresult: 1 2 3 4 5 6

This output would occur if the `echo` command simply printed the original line or if variables were read and printed in their original order.

Cresult: 6 5 4

This output is incorrect because `read` assigns "1" to `a`, "2" to `b`, and the remainder "3 4 5 6" to `c`, so `c` contains more than just "3".

Dresult: 6 5 4 3 2 1

This output would require a different parsing logic or `read` behavior where each word is individually assigned and then reversed, which is not how `read a b c` handles multiple words when there are more words than variables.

Eresult: 3 2 1

This output is incorrect; variable `c` would contain "3 4 5 6", not just "3".

Concept tested: Shell read command variable assignment

Source: https://www.gnu.org/software/bash/manual/bash.html#index-read

Topics

#shell scripting#bash#pipelines#read command

Community Discussion

No community discussion yet for this question.

Full LX0-104 Practice