You run a command piped into grep, awk, or a loop, and your terminal hangs with zero output, even though the source program prints lines every second. Then, when you finally hit Ctrl+C, twenty lines flush to the screen all at once before the process dies.
text$ tail -f /var/log/app/events.log | grep --line-buffered "ERROR" | cut -d' ' -f2 | tr 'a-z' 'A-Z'
^C
PAYLOAD_FAILED
DB_TIMEOUT
AUTH_DROP
$
The data was there the whole time. Your pipeline simply swallowed it until the buffer filled up or the process terminated.
The Standard IO Buffer Boundary
When a program writes to standard output connected to an interactive terminal, the C standard library defaults to line buffering. Each newline sends the buffer down the wire to your display immediately.
The moment you redirect that output into a pipe (|), the standard library switches the buffer mode from line-buffered to fully buffered (block buffered). Instead of sending bytes on every \n, it waits until it accumulates a block of memory, typically 4096 bytes (4 KB) or 65536 bytes (64 KB) depending on the operating system and libc implementation.
If your upstream process produces low-volume logs at ten bytes per second, a 4 KB buffer takes over four hundred seconds to flush. To a human watching the terminal, the pipe looks dead.
Adding --line-buffered to grep only fixes grep. It does not fix the utility running downstream from grep, nor does it fix the utility feeding upstream into it. Every single stage in a POSIX pipeline maintains its own independent buffer on both stdin and stdout. If one tool in the chain sits on a 4 KB block, the entire pipeline stalls.
Modifying Buffer Behavior with stdbuf
The core utilities package provides stdbuf. It works on Linux and FreeBSD by preloading a shared library (libstdbuf.so) through LD_PRELOAD that overrides the default buffer settings initialized during main().
To force a command to use line buffering on standard output, run it through stdbuf -oL:
bashstdbuf -oL cut -d' ' -f2
To force unbuffered standard output entirely (every single character passed through immediately, useful for raw binary or single-character prompts):
bashstdbuf -o0 tr 'a-z' 'A-Z'
Here is the broken pipeline rewritten so that every stage flushes on newlines:
bashtail -f /var/log/app/events.log \
| grep --line-buffered "ERROR" \
| stdbuf -oL cut -d' ' -f2 \
| stdbuf -oL tr 'a-z' 'A-Z'
If you use sed in the pipeline, pass its native unbuffered flag -u:
bashsed -u 's/foo/bar/'
If you use awk, call fflush() after printing, or pass -W interactive in GNU awk:
bashawk '{ print $2; fflush() }'
Emulating a Terminal with script or socat
stdbuf relies on standard dynamically linked C binaries. It fails silently or outright errors when applied to statically compiled binaries (like those produced by Go or Rust), setuid binaries, or scripts with internal runtimes like Python when Python overrides its own buffer settings.
When stdbuf cannot inject itself, the standard workaround is to trick the source process into believing it is talking to a real terminal instead of a pipe. Real terminals enforce line buffering by default.
On Linux, use script with the -q (quiet) and -c (command) flags, sending output to /dev/null:
bashscript -q -c "my-static-go-binary" /dev/null | grep "MATCH"
The script utility allocates a pseudo-terminal (pty). The upstream tool detects an interactive tty on file descriptor 1 and flushes every line.
On systems where script syntax differs (such as macOS/BSD), you can use socat to bridge a pty directly to stdout:
bashsocat -u EXEC:"my-static-go-binary",pty,raw STDOUT | grep "MATCH"
Verifying Pipeline Buffer Modes
Do not guess whether a process is buffering. You can inspect the standard file descriptors of a running pipeline using lsof or /proc.
Start a test pipeline in one terminal window:
bashtail -f /dev/urandom | tr -cd 'a-z\n' | grep --line-buffered "a" > /tmp/test.out &
Find the Process IDs (PIDs):
bashpgrep -f "tr -cd"
pgrep -f "grep --line-buffered"
Check the open file descriptors of the tr process (replace 12345 with the PID of tr):
bashls -l /proc/12345/fd
textlrwx------ 1 user user 64 Feb 18 10:14 0 -> /dev/pts/1
l-wx------ 1 user user 64 Feb 18 10:14 1 -> 'pipe:[7849201]'
lrwx------ 1 user user 64 Feb 18 10:14 2 -> /dev/pts/1
Notice that file descriptor 1 points to pipe:[7849201]. Because it points to a FIFO node and not /dev/pts/*, glibc enforces a 4096-byte buffer unless overridden.
Now inspect the buffer size directly using gdb without killing the process:
bashgdb -p 12345 --batch -ex "p *(FILE*)stdout"
Look for the internal _IO_buf_end and _IO_buf_base pointers in the output:
text$1 = {_flags = -72539004, _IO_read_ptr = 0x0, _IO_read_end = 0x0,
_IO_read_base = 0x0, _IO_write_base = 0x55dc1b3a2000 "",
_IO_write_ptr = 0x55dc1b3a2150 "", _IO_write_end = 0x55dc1b3a3000 "",
_IO_buf_base = 0x55dc1b3a2000 "", _IO_buf_end = 0x55dc1b3a3000 ""}
Subtract _IO_buf_base from _IO_buf_end: 0x55dc1b3a3000 minus 0x55dc1b3a2000 is 0x1000 in hex, or 4096 bytes in decimal. That confirms the process is buffering 4 KB before flushing.
Now run the same program wrapped in stdbuf:
bashstdbuf -oL tr -cd 'a-z\n' | grep --line-buffered "a" &
Inspect the new PID with gdb:
bashgdb -p 12350 --batch -ex "p *(FILE*)stdout"
text$1 = {_flags = -72540028, _IO_read_ptr = 0x0, _IO_read_end = 0x0,
_IO_read_base = 0x0, _IO_write_base = 0x55dc1b3a2000 "",
_IO_write_ptr = 0x55dc1b3a2000 "", _IO_write_end = 0x55dc1b3a2000 "",
_IO_buf_base = 0x55dc1b3a2000 "", _IO_buf_end = 0x55dc1b3a2001 ""}
The difference between base and end is now 1 byte, meaning the runtime flushes every single character or newline immediately.
Edge Case 1: Python and Perl Runtimes in the Middle of a Pipe
If you run a Python script inside a pipeline, stdbuf will often fail to alter its behavior. Python configures its own standard stream objects (sys.stdout) on startup and ignores the C runtime defaults set by LD_PRELOAD.
Given this script:
python# log_filter.py
import sys
for line in sys.stdin:
if "ERROR" in line:
sys.stdout.write(line)
Placing this in a pipe replicates the hang:
bashtail -f /var/log/app/events.log | python3 log_filter.py | grep "CRITICAL"
Fix this by passing the -u (unbuffered) flag directly to the Python interpreter binary:
bashtail -f /var/log/app/events.log | python3 -u log_filter.py | grep "CRITICAL"
Or set the environment variable in the execution subshell:
bashtail -f /var/log/app/events.log | PYTHONUNBUFFERED=1 python3 log_filter.py | grep "CRITICAL"
If you are modifying the Python script itself, write to sys.stdout and call flush explicitly, or use print(..., flush=True):
python# log_filter.py
import sys
for line in sys.stdin:
if "ERROR" in line:
sys.stdout.write(line)
sys.stdout.flush()
For Perl, the syntax is setting the special autoflush variable $| to 1:
bashtail -f /var/log/app/events.log | perl -pe 'BEGIN { $| = 1 } s/foo/bar/' | grep "bar"
Edge Case 2: Named Pipes (FIFOs) and Read Deadlocks
When you decouple pipeline stages using POSIX named pipes (mkfifo), you hit a different buffering issue: blocking opens and pipe buffer limits.
bashmkfifo /tmp/event_pipe
tail -f /var/log/app/events.log > /tmp/event_pipe &
cat /tmp/event_pipe | grep "ERROR"
If the reader process (grep) terminates or restarts, the writer process (tail) receives a SIGPIPE signal and crashes. Worse, if your upstream script opens the FIFO in write-only mode (>), it blocks completely until a reader process opens the other end.
text$ echo "event payload" > /tmp/event_pipe
# Terminal freezes here indefinitely until another process reads /tmp/event_pipe
To prevent a pipe from locking the shell script when no consumer is attached, open the FIFO in read-write mode using an explicit file descriptor:
bashexec 3<>/tmp/event_pipe
echo "event payload" >&3
Because file descriptor 3 holds the pipe open for both reading and writing, the kernel never registers zero readers. Writes succeed immediately without hanging the process, up to the OS pipe capacity limit (65,536 bytes on Linux).
You can check and expand this buffer limit directly using fcntl via Python if your pipeline drops lines under high throughput:
bashpython3 -c '
import fcntl, os
F_SETPIPE_SZ = 1031
fd = os.open("/tmp/event_pipe", os.O_RDWR)
fcntl.fcntl(fd, F_SETPIPE_SZ, 1048576) # Set to 1MB
print("Buffer size set to 1MB")
'
Verify the capacity:
bashpython3 -c '
import fcntl, os
F_GETPIPE_SZ = 1032
fd = os.open("/tmp/event_pipe", os.O_RDWR)
print(fcntl.fcntl(fd, F_GETPIPE_SZ))
'
text1048576