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…
Question
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)- A78% (35)
- B4% (2)
- C2% (1)
- D13% (6)
- E2% (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 assumesc=6,b=5,a=4as if tokens were assigned from the end, whichreadnever does. - D (
result: 6 5 4 3 2 1) assumes a full reversal of all six tokens, butreadonly 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 thatcabsorbs 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
Community Discussion
No community discussion yet for this question.