LFCS · Question #547
LFCS Question #547: Real Exam Question with Answer & Explanation
The correct answer is A: result: 3 4 5 6 2 1. The echo command pipes a single line of space-separated numbers to a while read loop, which assigns the first two words to a and b, and all remaining words to c, then echoes them in reverse order.
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
Explanation
The echo command pipes a single line of space-separated numbers to a while read loop, which assigns the first two words to a and b, and all remaining words to c, then echoes them in reverse order.
Common mistakes.
- B. This output implies the variables were not assigned correctly or were echoed in their original order.
- C. This output only shows the last three numbers, indicating an incorrect understanding of how
readhandles multiple words with multiple variables. - D. This output implies a complete reversal of all numbers, which would only happen if each number was read into a separate variable and then processed.
- E. This output implies only the first three numbers were processed and reversed, but
readassigns all remaining words to the last variable.
Concept tested. Bash read command behavior
Reference. https://www.gnu.org/software/bash/manual/bash.html#index-read
Topics
Community Discussion
No community discussion yet for this question.