Force line-buffering of stdout in a pipeline Force line-buffering of stdout in a pipeline unix unix

Force line-buffering of stdout in a pipeline


you can try stdbuf

$ stdbuf --output=L ./a | tee output.txt

(big) part of the man page:

  -i, --input=MODE   adjust standard input stream buffering  -o, --output=MODE  adjust standard output stream buffering  -e, --error=MODE   adjust standard error stream bufferingIf MODE is 'L' the corresponding stream will be line buffered.This option is invalid with standard input.If MODE is '0' the corresponding stream will be unbuffered.Otherwise MODE is a number which may be followed by one of the following:KB 1000, K 1024, MB 1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y.In this case the corresponding stream will be fully buffered with the buffersize set to MODE bytes.

keep this in mind, though:

NOTE: If COMMAND adjusts the buffering of its standard streams ('tee' doesfor e.g.) then that will override corresponding settings changed by 'stdbuf'.Also some filters (like 'dd' and 'cat' etc.) dont use streams for I/O,and are thus unaffected by 'stdbuf' settings.

you are not running stdbuf on tee, you're running it on a, so this shouldn't affect you, unless you set the buffering of a's streams in a's source.

Also, stdbuf is not POSIX, but part of GNU-coreutils.


Try unbuffer which is part of the expect package. You may already have it on your system.

In your case you would use it like this:

./a | unbuffer -p tee output.txt

(-p is for pipeline mode where unbuffer reads from stdin and passes it to the command in the rest of the arguments)


You may also try to execute your command in a pseudo-terminal using the script command (which should enforce line-buffered output to the pipe)!

script -q /dev/null ./a | tee output.txt     # Mac OS X, FreeBSDscript -c "./a" /dev/null | tee output.txt   # Linux

Be aware the script command does not propagate back the exit status of the wrapped command.