stdbuf

stdbuf runs a command with altered buffering on its standard streams.

stdbuf [options] command...
$ slow-producer | stdbuf -oL grep error

What buffering is, and why change it #

A program usually does not write each piece of output the instant it is produced — it collects output in a buffer and writes it in batches, which is faster. The cost is delay: when a program's output feeds a pipe, that batching can hold lines back for a long time, which is unhelpful when you are watching a pipeline live.

stdbuf lets you override that batching for a command, so its output appears sooner.

Setting the buffering #

OptionStream
-i, --input=MODEStandard input.
-o, --output=MODEStandard output.
-e, --error=MODEStandard error.

MODE is one of:

MODEBuffering
0Unbuffered — every write goes out immediately.
LLine-buffered — output is flushed at the end of each line. Not valid for input.
a sizeFully buffered with a buffer of that many bytes. Accepts suffixes — K, M, and so on.

-oL — line-buffered output — is the common case: it makes a command in a pipeline emit each line as it is finished.

A limitation #

stdbuf adjusts the default buffering. A command that manages its own stream buffering will override what stdbuf sets, and some commands do not use buffered streams at all — for those, stdbuf has no effect.

Exit status #

When stdbuf runs a command, it exits with that command's status. The exception is a failure in stdbuf itself:

CodeMeaning
(command's own)The command ran; this is its exit status.
(a stdbuf-level error code)The command could not be started.

Edit this page