LFCS Exam QuestionsBrowse all questions from this exam

LFCS Exam - Question 112


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

Show Answer
Correct Answer: A

The command sequence begins by echoing the string '1 2 3 4 5 6', which is then piped into a while loop. The 'read' command within the while loop reads the first value into variable 'a', the second value into variable 'b', and the remaining values into variable 'c'. Thus, 'a' is '1', 'b' is '2', and 'c' is '3 4 5 6'. The loop then echoes the values of 'c', 'b', and 'a' in that order, resulting in the output 'result: 3 4 5 6 2 1'.

Discussion

7 comments
Sign in to comment
boorceOption: A
Mar 28, 2024

Confirm in bash: a='1' b='2' c='3 4 5 6' So echo result: $c $b $a => result: 3 4 5 6 2 1

BorbzOption: A
Feb 24, 2021

the answer is correct, tested in lab. But i can't understand the logic of it... can anyone explain?

Borbz
Feb 24, 2021

ok, i got the logic. The "read a" records the first value (1), "read b" records the second value (2) and the last read "read c" records all remaining values (3 4 5 6). therefore the output of $c $b $a is 3 4 5 6 2 1.

Ivandrago
Dec 20, 2021

a=1,b=2,c=3456 so echo 345621

KMAVOption: E
Apr 3, 2023

The given command sequence reads a string "1 2 3 4 5 6" using the echo command, then pipes it to a while loop that reads each of the space-separated values into the variables a, b, and c in turn. The loop then echoes the values of c, b, and a in that order, preceded by the string "result: ". Since the loop reads the values into a, b, and c in that order, and the loop echoes them in reverse order, the resulting output will be: result: 3 2 1

ARH2023Option: E
Nov 12, 2023

The given command sequence reads input numbers '1 2 3 4 5 6' and then uses a while loop to assign them to variables a, b, and c, respectively. It then echoes these variables in reverse order. Here's the output: makefile result: 3 2 1 So, the output will be "result: 3 2 1" for each iteration of the while loop, as it reverses the order of the input numbers.

EliteAllenOption: E
Jan 4, 2024

$ echo '1 2 3 4 5 6' | while read a b c; do > echo result: $c $b $a; > done result: 3 4 5 6 2 1

EliteAllen
Jan 4, 2024

Excuse me. Correct answer is A.

9866666Option: A
Jun 25, 2024

$ echo '1 2 3 4 5 6' | while read a b c; do echo result: $c $b $a; done result: 3 4 5 6 2 1