How to clean shell command

is there an equivalent to the C function fflush() for a shell command?

 echo "kkkk" flush <<< are there some command to execute to flush stdout? 
+5
source share
2 answers

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.

0
source

No, you will never need it.

When a program, such as echo , exits, all output is automatically cleared. If the program has not yet been released, then flushing is internal and has nothing to do with the shell.

-2
source

Source: https://habr.com/ru/post/1490315/


All Articles