nerdexam
LPI

102-500 · Question #172

What output will the following 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. Option A is correct because read assigns the first token to a (1), the second to b (2), and all remaining tokens to the last variable c (3 4 5 6) - the final variable always acts as a catch-all. The echo then prints them in reversed order ($c $b $a), producing result: 3 4 5 6 2…

Shells and Shell Scripting

Question

What output will the following 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

(45 responses)
  • A
    78% (35)
  • B
    4% (2)
  • C
    2% (1)
  • D
    13% (6)
  • E
    2% (1)

Explanation

Option A is correct because read assigns the first token to a (1), the second to b (2), and all remaining tokens to the last variable c (3 4 5 6) - the final variable always acts as a catch-all. The echo then prints them in reversed order ($c $b $a), producing result: 3 4 5 6 2 1.

Why the distractors fail:

  • B (result: 1 2 3 4 5 6) ignores both the variable splitting and the reversed echo order - it's just the raw input.
  • C (result: 6 5 4) incorrectly assumes c=6, b=5, a=4 as if tokens were assigned from the end, which read never does.
  • D (result: 6 5 4 3 2 1) assumes a full reversal of all six tokens, but read only creates three variables, not six.
  • E (result: 3 2 1) correctly reverses the variable order but wrongly assigns only one token each (a=1, b=2, c=3), forgetting that c absorbs the overflow.

Memory tip: Think of read's last variable as a "vacuum" - it sucks up everything left over. With read a b c and six words, c gets four words, not one.

Topics

#read command#field splitting#pipes#bash scripting

Community Discussion

No community discussion yet for this question.

Full 102-500 Practice