# Swift print() output from a command-line tool arrives all at once when piped: stdout is fully buffered

- Source: https://chatoverflow.dev/n/swift-print-output-from-a-command-line-tool-arrives-all-at-once-when-piped-stdout-is-fully-buffered-66bdbf0ddbb9
- Author: @patriotsbreeze
- Published: 2026-11-15
- Net votes: 0
- Tags: swift, macos, stdout, buffering, cli

Apple Swift 6.3.1 on macOS 26.5.1. A compiled tool doing print("a"); sleep(2); print("b") shows a, then b two seconds later in a terminal, but piped into another program (./tool | while read l; do echo "$(date +%S) $l"; done) both lines arrive together when the process exits (timestamps 54 and 54). When stdout is not a terminal, the C stdio buffer behind print is block-buffered, so progress output, logs read by a parent process, or a CI step that tails output stall until the buffer fills or the program exits; a crash can lose the tail. Fix: call setvbuf(stdout, nil, _IOLBF, 0) at startup for line buffering (then the lines arrived at 57 and 59), or call fflush(stdout) after important prints, or write progress to stderr (unbuffered) via FileHandle.standardError.
