stdbuf
Control I/O buffering for standard streams
TLDR
Change stdin buffer size to 512 KiB
Change stdout buffer to line-buffered
Change stderr buffer to unbuffered
SYNOPSIS
stdbuf -i0|-oL|-eL COMMAND [ARG...]
stdbuf --input=0|L|N --output=0|L|N --error=0|L|N COMMAND [ARG...]
PARAMETERS
-i MODE, --input=MODE
Set stdin buffering to MODE.
-o MODE, --output=MODE
Set stdout buffering to MODE.
-e MODE, --error=MODE
Set stderr buffering to MODE.
MODE
Specifies the buffering type:
0: Unbuffered. Input/output is immediate.
L: Line-buffered. Input/output is flushed after each newline.
N or B[SIZE]: Full-buffered. N is an integer representing the buffer size in bytes. B with an optional SIZE suffix (e.g., B1M for 1 Megabyte). If B is used without a size, it defaults to the system's preferred full-buffer size.
COMMAND
The command to execute with modified buffering.
ARG...
Arguments passed to the COMMAND.
--help
Display help and exit.
--version
Display version information and exit.
DESCRIPTION
stdbuf is a command-line utility used to modify the buffering behavior of standard input (stdin), standard output (stdout), and standard error (stderr) for another command. By default, many programs buffer their I/O for efficiency. For instance, stdout is often line-buffered when connected to a terminal and fully-buffered when piped to another command or redirected to a file.
This default behavior can sometimes be undesirable, especially in pipelines where immediate output is required, or when debugging. stdbuf allows users to force a specific buffering mode: unbuffered (data written immediately), line-buffered (data written after each newline character), or full-buffered (data written when a buffer is full, or a specific buffer size is reached). It achieves this by preloading a library that intercepts standard C library I/O functions like setvbuf(). This provides fine-grained control over how data flows through command pipelines, making it an invaluable tool for scripting, debugging, and optimizing I/O.
CAVEATS
stdbuf works by preloading a library (libstdbuf.so) that intercepts standard C library stream functions like setvbuf(). If the target COMMAND does not use these standard library functions for its I/O (e.g., it performs raw system calls like read()/write()), or if it explicitly overrides buffering in a way that bypasses setvbuf(), then stdbuf may have no effect. It only influences the buffering of standard streams (stdin, stdout, stderr), not arbitrary file descriptors or files opened by the program itself.
ENVIRONMENT VARIABLES
stdbuf works by setting the LD_PRELOAD environment variable to point to its helper library (libstdbuf.so) before executing the target command. This mechanism allows it to override default library function behavior without modifying the command itself.
BUFFERING TYPES EXPLAINED
Unbuffered (0): Data is sent to the kernel (and potentially to the output device) as soon as it's written by the program. Least efficient but provides the most immediate feedback.
Line-buffered (L): Data is buffered until a newline character is encountered, the buffer is full, or fflush() is called. This is common for interactive terminal applications.
Full-buffered (N or B): Data is buffered until the buffer is full, or fflush() is called. Most efficient for bulk data transfer, common when output is redirected to a file or pipe.
HISTORY
The stdbuf command is part of the GNU Core Utilities (coreutils), a package of essential command-line tools for Unix-like operating systems. It was introduced to provide a more convenient and portable way to control stream buffering compared to platform-specific environment variables (e.g., GZIP_DEFLATE_BUFSIZE) or writing custom C wrappers around commands using setvbuf(). Its development aimed to address common issues in scripting and piping where unexpected buffering behavior could lead to delays or incorrect processing.
SEE ALSO
unbuffer(1) (from expect package), dd(1), setvbuf(3), stdio(3)