8

are there an equivalent of the C function fflush() for shell command?

echo "kkkk"
flush <<< are there some command to execute to flush stdout?
MOHAMED
  • 41,599
  • 58
  • 163
  • 268

2 Answers2

-1

Short answer: There is no need to perform explicit flush for shell scripts. All shell output (via echo) is NOT buffered. The output is immediately sent to the output device. The situation is more complex if the output is piped into another program, which may buffer it's output.

Long answer: consider

(echo aaa ; sleep 3  ; echo bbb)

Which will display 'aaa', and after 3 seconds delay, 'bbb' - since the pipe construct will result in the the C program using buffered output.

As noted above, if the output is send (via pipe) to a program which will buffer the output, the first line may be delayed, and displayed at the same time as the second line (after the 3 seconds delay). The solution is to force "line buffering" (or no buffering) on other programs in the pipeline.

For example given a simple "C" program to copy stdin to stdout:

int main() { char b[256] ; while (fgets(b, sizeof(b), stdin) ) fputs(b, stdout) };

and running

(echo aaa ; sleep 3 ; echo bbb  ) | a.out | cat

Will result in buffering in a.out, showing 'aaa' and 'bbb' after 3 seconds delay.

dash-o
  • 13,723
  • 1
  • 10
  • 37
-2

No, you should never need it.

When a program, such as echo, exits, all output data is automatically flushed. If the program has not exited yet then flushing is an internal matter and nothing to do with the shell.

ams
  • 24,923
  • 4
  • 54
  • 75
  • 4
    Well, echo is a bash builtin – Pavel Anossov Jul 08 '13 at 14:59
  • @PavelAnossov: only for efficiency. If bash didn't do that then /bin/echo would do the same job. – ams Jul 08 '13 at 15:10
  • @ams: But what if the intention is to update the progres counter sitting on the same line like `printf "\rCounter %02d" "$cnt"`. Here you do not want to do a newline (only the carriage return). Now it needs to be flushed. How to do that? – pepr Nov 27 '13 at 14:08
  • 1
    @pepr you still don't need fflush. Incidentally, you need `echo -n` to not print a line-feed. – ams Nov 27 '13 at 17:37
  • 2
    Output can be buffered if piped through `tee`, for example. So it's not always _automatically_ flushed. – Benoit Duffez Oct 22 '15 at 09:11
  • @BenoitDuffez In the case of `tee`, it will flush through that very quickly also, but yes, it's automatic unless you deliberately break it. – ams Oct 22 '15 at 09:26
  • 3
    @ams: Not true, piping creates a 4K buffer, so output will be sent to `tee` *only* when it reaches 4K. This will delay printing, and create the need to flush the buffer sooner. `stdbuf` can do that (GNU coreutils). – Benoit Duffez Oct 22 '15 at 09:28
  • @BenoitDuffez No, when `echo` exits the buffer will be flushed. Then `tee` will also exit, and its buffer will be flushed. And anyway, `tee` runs in a line-buffered mode when run from a terminal. – ams Oct 22 '15 at 09:44
  • 2
    If you pipe a block of commands in a script, output will be buffered with a 4K buffer. Output will be delayed. – Benoit Duffez Oct 22 '15 at 11:34
  • Yes, non interactive stuff is not line-buffered. I don't believe any of this is relevant to the two-year-old question. – ams Oct 22 '15 at 11:51
  • 1
    I stumbled upon it, its age is irrelevant. I saw incorrect or incomplete information and tried to point it out. – Benoit Duffez Oct 23 '15 at 13:25
  • You decided to tell somebody that their sports car isn't going to work off-road. Not helpful. – ams Oct 23 '15 at 13:35
  • 1
    @BenoitDuffez You comment is very relevant and it helped me debug a difficult bug. Thanks! – quant_dev Mar 07 '16 at 17:35