-3

I piped one echo command into the other

🌐 echo a b c d e f g h i | echo 

🌐 echo $?
0

Contrary to my intuition, there was no output, however there was also no error returned. I expected, that echo a b c d | echo is only an unnecessary redundant alternative to echo a b c d . But it is not the case, Why were the arguments lost on their way through the pipe?

marked as duplicate by Kusalananda, don_crissti, Stéphane Chazelas, ilkkachu, Jeff Schaller May 27 '17 at 13:15

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

6

This is due to echo not reading from standard input. Pipes are only useful for sending the standard output from one command to the standard input of the next command.

Since the output ef echo a b c ... is not consumed by the second echo, it is lost and there is no output from the pipe, except for the single newline from the second echo.

Since the last echo successfully outputs a blank line, the exit status is zero.

  • if echo does not read from standard in, how does xargs put the arguments into the second echo command: echo a b c d e f g | xargs -n2 echo ? – sharkant May 27 '17 at 12:07
  • 1
    That's what xargs is paid to do. It reads items from standard input and runs the command given as argument to xargs with the collected list of items as arguments to the command. In you example, xargs reads "a b c d e f g", and runs "echo a b c d e f g". – Johan Myréen May 27 '17 at 12:21
  • @sharkant As Johan said. xargs does read from standard input. – Kusalananda May 27 '17 at 12:27
  • 1
    Or just use cat instead of echo in second pipe. Cat without arguments reads and copies stdin – Sergiy Kolodyazhnyy May 27 '17 at 12:29
  • @SergiyKolodyazhnyy Or just drop tho pipe and the second command completely. – Kusalananda May 27 '17 at 12:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.