Given this minimal example
( echo "LINE 1" ; sleep 1 ; echo "LINE 2" ; )
it outputs LINE 1
and then, after one second, outputs LINE 2
, as expected.
If we pipe this to grep LINE
( echo "LINE 1" ; sleep 1 ; echo "LINE 2" ; ) | grep LINE
the behavior is the same as in the previous case, as expected.
If, alternatively, we pipe this to cat
( echo "LINE 1" ; sleep 1 ; echo "LINE 2" ; ) | cat
the behavior is again the same, as expected.
However, if we pipe to grep LINE
, and then to cat
,
( echo "LINE 1" ; sleep 1 ; echo "LINE 2" ; ) | grep LINE | cat
there is no output until one second passes, and both lines appear on the output immediately, which I did not expect.
Why is this happening and how can I make the last version to behave in the same way as the first three commands?
cat
concatenates files. What are you trying to do by piping intocat
? – Douglas Held Sep 05 '18 at 20:09cat
simply readsstdin
and outputs intostdout
. Of course, I came up with this question with a lot of complex stuff in place ofecho
andcat
, but these turned out to be irrelevant, since the problem shows up with much simpler examples. – lisyarus Sep 05 '18 at 21:11