TECH Signal 400
Piped processes reclaim terminal input by opening /dev/tty
A Unix process whose stdin is connected to a pipe can still read keyboard input by opening /dev/tty, which resolves to its controlling terminal independently of fd 0.
Tools like less and fzf already use this technique to combine piped data with interactive input, but the mechanism is not widely understood; knowing it enables writing pipeline stages that accept terminal interaction while processing piped data.
Written by elseif from the cluster below · every claim links back to a sourceThe three things worth knowing
A process can infer its position in a pipeline by checking whether its stdin and stdout are terminals, eliminating the need for explicit stage numbering.
Downstream pipeline stages drain their stdin pipe, which blocks until the upstream process exits and closes the pipe, providing natural coordination without a separate signal.
Opening /dev/tty creates a new file descriptor to the controlling terminal, letting a piped process read keyboard input even when fd 0 is connected to a pipe.
THE CLUSTER
↗