
You chain a few Unix commands together to monitor live server logs. The syntax is flawless:
tail -f access.log | grep --color=never "POST /api/checkout" | awk '{print $1, $4, $7}'
You hit Enter. Things look promising for a fraction of a second. Then, absolute silence. The cursor sits blinking. You know requests are hitting the server because your monitoring dashboard is lighting up. Yet your terminal refuses to emit a single line.
Five minutes later, you hit Ctrl+C. Suddenly, a massive wall of fifty lines floods your screen all at once before the terminal returns to your prompt.
You have just been bitten by I/O buffering.
Strip ANSI colors, prompts, spinners & code fences from copied logs
Sanitize copied terminal logs, AI agent CLI outputs, and shell commands. Strip ANSI color codes, $ prompts, code fences, line numbers, and spinners.
In interactive terminal usage, programs flush their output after every newline (\n). But the moment you connect a program's standard output to a Unix pipe (|), the C standard library (libc) automatically switches from line buffering to full block buffering (typically 4,096 or 8,192 bytes). Nothing reaches downstream tools until that 4KB–8KB buffer fills up completely.
This guide examines the Unix standard I/O layer to inspect how standard I/O behaves under the hood, examine the kernel and userland buffering tiers, and explore every tool and flag needed to keep your pipelines streaming in real time.
When troubleshooting pipeline latency, developers often confuse userland stream buffering with kernel pipe capacity. Both exist, but they operate at completely different layers of the operating system.
Process A (User Space) Kernel Space Process B (User Space)
┌───────────────────────┐ ┌─────────────────────────┐ ┌───────────────────────┐
│ Application Code │ │ │ │ Application Code │
│ └─ printf("hello\n") │ │ │ │ └─ fgets(...) │
│ │ │ │ │ │ ▲ │
│ ┌────────▼──────────┐ │ │ │ │ ┌────────┴──────────┐ │
│ │ libc stdio buffer │ │ │ │ │ │ libc stdio buffer │ │
│ │ (4KB - 8KB) │ │ │ │ │ │ (4KB - 8KB) │ │
│ └────────┬──────────┘ │ │ │ │ └────────▲──────────┘ │
└──────────┼────────────┘ │ │ └──────────┼────────────┘
│ write(fd, buf) │ │ │ read(fd, buf)
▼ │ │ │
┌──────────────────────────────┴─────────────────────────┴───────────────┴────────────┐
│ Linux Kernel Pipe Ring Buffer (Default: 65,536 bytes / 16 memory pages) │
└─────────────────────────────────────────────────────────────────────────────────────┘
FILE* in libc)The standard C library (glibc, musl) wraps raw kernel file descriptors with high-level FILE* streams (stdin, stdout, stderr). System calls (write(2) and read(2)) are computationally expensive because they require switching the CPU between user mode and kernel mode.
To optimize throughput, libc provides three buffering disciplines defined in <stdio.h>:
_IONBF): Characters are transmitted to the kernel immediately via write() as soon as they are written. stderr uses this by default so that error messages appear even if the process crashes immediately afterward._IOLBF): Characters are accumulated until a newline (\n) is encountered, at which point libc flushes the buffer to the kernel with a single write() call._IOFBF): Characters are accumulated until a fixed-size buffer (defined by BUFSIZ, typically 4,096 or 8,192 bytes) is full. Newlines are treated as ordinary characters and do not trigger a flush.libc Decides: The isatty(3) SyscallWhen a program starts, libc invokes isatty(fileno(stdout)) to inspect where standard output is pointing:
// Simplified pseudo-code inside libc initialization:
if (isatty(STDOUT_FILENO)) {
// Standard output is connected to an interactive terminal (PTY/TTY)
setvbuf(stdout, NULL, _IOLBF, BUFSIZ); // Line buffering
} else {
// Standard output is redirected to a file or a PIPE (|)
setvbuf(stdout, NULL, _IOFBF, BUFSIZ); // Full block buffering!
}
This explains the mystery: The exact same binary behaves differently when run alone versus when placed before a pipe!
When you run tail -f file | grep "pattern", grep's output is not a TTY - it is connected to awk via an anonymous pipe. Therefore, grep's libc automatically switches to block buffering, holding onto every matched line until 4,096 bytes accumulate before passing them to awk.
Most standard Unix utilities provide flags to force unbuffered or line-buffered output. Keep this cheat sheet handy:
| Command | Flag / Option | Description |
|---|---|---|
grep | --line-buffered | Flushes output on every matched newline. |
sed | -u or --unbuffered | Flushes output after processing every line. |
awk | fflush() | Explicitly flushes standard output inside print statements. |
jq | --unbuffered | Flushes JSON output after every emitted item. |
tcpdump | -l | Makes stdout line-buffered so packets print immediately in pipes. |
tr | -u (on BSD/macOS) | Unbuffered translation. |
cut | No native flag | Requires stdbuf or rewriting via awk. |
sort | Impossible in stream | Must consume EOF before sorting; cannot stream unbounded pipes. |
Let us revisit the stuck pipeline from the introduction:
# ❌ FROZEN PIPELINE: grep and awk will block-buffer
tail -f access.log | grep "POST /api/checkout" | awk '{print $1, $4, $7}'
# ✅ REAL-TIME PIPELINE: Instant streaming
tail -f access.log | grep --line-buffered "POST /api/checkout" | awk '{print $1, $4, $7; fflush()}'
Notice that every intermediary stage in the pipeline must be unbuffered. If grep flushes line-by-line but awk block-buffers, the pipeline still freezes at awk.
If you write helper scripts that participate in pipelines, configure stdout buffering explicitly.
Python standard library defaults to block buffering when stdout is redirected. You have three primary ways to force line buffering:
-u flag: python3 -u script.py | consumerexport PYTHONUNBUFFERED=1import sys
# Python 3.7+ idiomatic configuration
sys.stdout.reconfigure(line_buffering=True)
# Or explicitly flush on individual print statements:
print("Event processed", flush=True)
In C, call setvbuf() at the very start of main():
#include <stdio.h>
int main(void) {
// Force line buffering even when redirected to a pipe
setvbuf(stdout, NULL, _IOLBF, 0);
// Or disable buffering entirely:
// setvbuf(stdout, NULL, _IONBF, 0);
printf("Immediate output\n");
return 0;
}
In C++, std::endl automatically inserts a newline and flushes the stream (std::cout << "msg" << std::endl;). Alternatively, enable std::unitbuf:
#include <iostream>
int main() {
std::ios_base::sync_with_stdio(false);
std::cout << std::unitbuf; // Flushes after every insertion
return 0;
}
In Node.js, process.stdout.write() is asynchronous on POSIX streams when writing to a pipe, but Node does not perform line buffering:
// In Node.js, process.stdout handles buffering internally.
// If you need immediate transmission:
process.stdout.write(data + '\n');
In Go, standard fmt.Println writes directly to the underlying file descriptor (os.Stdout), which does not buffer by default. However, if you wrap stdout in bufio.Writer, ensure you flush:
writer := bufio.NewWriter(os.Stdout)
writer.WriteString("streaming message\n")
writer.Flush() // Essential!
In Ruby:
$stdout.sync = true # Disables full buffering globally
In Perl:
$| = 1; # Autoflush the currently selected output handle
stdbuf and unbufferWhat happens if you are using an old binary, a proprietary closed-source CLI, or a tool like cut that lacks a line-buffering flag?
You have two powerful system utilities:
stdbuf (The Elegant Solution)stdbuf is part of GNU Coreutils and available on virtually every Linux distribution. It modifies stream buffering without touching the source code:
-i: Standard input mode (0 for unbuffered, L for line-buffered, or a size like 1M)-o: Standard output mode (0, L, or size)-e: Standard error mode (0, L, or size)# Force cut and tr to be line-buffered in a live pipeline
tail -f access.log | stdbuf -oL cut -d' ' -f1,4,7 | stdbuf -oL tr '[:lower:]' '[:upper:]'
How stdbuf works under the hood:
When you run stdbuf -oL cmd, it sets the environment variable LD_PRELOAD=/usr/lib/coreutils/libstdbuf.so and passes _STDBUF_O=L. Before cmd's main() function is called, libstdbuf.so's constructor executes, parsing the environment variables and calling setvbuf(stdout, NULL, _IOLBF, 0). It is completely transparent to the target application.
unbuffer (The Universal PTY Hack)If a program statically links libc (e.g., compiled with Go or musl) or deliberately resets its own buffering internally, LD_PRELOAD will not work.
unbuffer (bundled with the expect package) creates a pseudo-terminal (PTY) device and executes the program inside it:
# Install expect package if not present:
# sudo apt-get install expect
unbuffer some_stubborn_cli | grep "ERROR"
Because the process is connected to a pseudo-terminal, isatty(STDOUT_FILENO) returns 1 (true). The program genuinely believes it is rendering to an interactive human terminal and enables line buffering naturally.
strace and /procWhen a pipeline appears hung in production, how do you verify whether it is waiting for input or stuck in a buffer?
straceAttach strace to the running process PID to inspect what system calls are firing:
# Find the PID of grep in your pipeline
pgrep -f "grep --color=never"
# Trace read and write system calls
strace -p <PID> -e trace=read,write
read(0, ...) returning data continuously, but zero write(1, ...) calls, the process is receiving data and accumulating it inside a userland libc buffer!write(1, "...", 4096) = 4096 firing intermittently in large blocks, block buffering is confirmed.write(1, "...", 80) = 80 firing on every line, your pipeline is successfully line-buffered./procLinux allocates an in-kernel circular buffer for every pipe. You can inspect its capacity and current fill level directly via /proc:
# Look up open file descriptors for the process
ls -l /proc/<PID>/fd/
# fd 0 -> pipe:[1492023]
# fd 1 -> pipe:[1492024]
# View pipe metadata and capacity (Linux 2.6.35+)
cat /proc/<PID>/fdinfo/1
Output:
pos: 0
flags: 01
mnt_id: 15
ino: 1492024
size: 0
By default, Linux kernel pipes hold 65,536 bytes (16 pages of 4,096 bytes). You can programmatically alter the kernel pipe capacity using the fcntl syscall with F_SETPIPE_SZ:
// Expand pipe capacity to 1MB to prevent upstream producers from blocking
fcntl(pipe_fd[1], F_SETPIPE_SZ, 1048576);
If buffering causes pipeline lag, why don't operating systems disable it everywhere by default?
The answer is throughput.
Every system call incurs CPU context-switching overhead: saving user registers, switching to kernel ring 0, validating memory pointers, updating scheduler state, and restoring user registers.
Consider processing a 10 GB Apache access log containing 50 million lines:
write() syscalls and 50,000,000 read() syscalls. The CPU spends over 70% of its execution cycles in kernel context switches. Total time: ~45 seconds.write() syscalls. System call overhead drops to near zero, allowing disk and CPU caches to run at maximum memory bandwidth. Total time: ~3.2 seconds.Use line buffering for live streaming, log tailing, interactive pipelines, and monitoring alerts where latency matters. Use block buffering for batch processing, log rotations, backups, and high-volume data transformations where throughput is king.
Are you processing live/streaming data (e.g. tail -f, tcpdump)?
│
├── NO (Batch data, large file) ──► Keep default block buffering (Fastest throughput)
│
└── YES (Real-time stream)
│
├── Does the tool have a line-buffering flag?
│ ├── YES ──► Use grep --line-buffered, sed -u, jq --unbuffered
│ └── NO
│ ├── Can you edit the source code?
│ │ ├── YES ──► Add setvbuf(stdout, NULL, _IOLBF, 0) or flush=True
│ │ └── NO
│ │ ├── Is dynamically linked glibc?
│ │ │ ├── YES ──► Wrap with: stdbuf -oL <cmd>
│ │ │ └── NO ──► Wrap with: unbuffer <cmd>
Why does grep switch from line buffering to block buffering when piped into awk?
Next time your terminal pipeline freezes while tailing a live file, do not rewrite your pipeline in another language.
Remember the two fundamental layers:
isatty(3) is false.--line-buffered, stdbuf -oL, or language-specific flush calls.Mastering stream buffering transforms ambiguous pipeline stalls into instantaneous, observable data streams.
Clean AI CLI logs, build cron expressions, decode JWTs, and calculate chmod permissions offline.

A minimal terminal setup that actually sticks: Starship prompt, zsh-autosuggestions, syntax highlighting, and the hard-learned lesson about the dotfile rabbit hole.
Read more
A no-fluff guide to SSH and SCP - covering port 22, key-based auth, the SSH config file, secure file transfers, and server hardening tips every developer should know.
Read more
Why 'q' quits almost everything, how Ctrl-C became a universal kill switch, and the POSIX ghosts that make the command line feel like home.
Read more