Short answer: There is no need to do explicit cleanup for shell scripts. All shell output (via echo) is NOT buffered. The output is immediately sent to the output device. The situation is more complicated if the output is transferred to another program that can buffer the output.
Long answer: consider
(echo aaa ; sleep 3 ; echo bbb)
Which will display 'aaa', and with a delay of 3 seconds 'bbb' - since the pipe construction will cause the C program to use buffered output.
As noted above, if the output is sent (via a channel) to a program that buffers the output, the first line can be delayed and displayed simultaneously with the second line (after a 3-second delay). The solution is to force the use of "linear buffering" (or not to buffer) for other programs in the pipeline.
For example, a simple βCβ program is provided for copying standard input to standard output:
int main() { char b[256] ; while (fgets(b, sizeof(b), stdin) ) fputs(b, stdout) };
and working
(echo aaa ; sleep 3 ; echo bbb ) | a.out | cat
Causes buffering in a.out, showing 'aaa' and 'bbb' after a 3 second delay.
source share