-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?

sharkant
  • 3,710

1 Answers1

7

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.

Kusalananda
  • 333,661
  • 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
  • so xargs does not push the arguments into the stdin of the echo command but makes sth. different to feed echo the arguments? – sharkant May 27 '17 at 14:02
  • @sharkant xargs will read from its own standard input and execute the given utility with the things that in reads as argument(s). – Kusalananda May 27 '17 at 23:36