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?