How to Use Named Pipes and Process Substitution Safely
FIFOs connect unrelated processes through a filesystem name; process substitution makes similar streams temporary. Both need careful lifecycle and failure handling.
An anonymous pipeline connects processes launched together. A named pipe, or FIFO, gives the kernel pipe a filesystem name so separately started processes can open the same stream.
Create a private FIFO
workdir=$(mktemp -d) || exit 1
fifo=$workdir/events
trap 'rm -rf -- "$workdir"' EXIT HUP INT TERM
mkfifo "$fifo" || exit 1
Using a private directory prevents another user from substituting a different object at a predictable path. The FIFO contains no stored file payload; writers and readers exchange bytes through the kernel while both ends are open.
Start both sides deliberately
Opening only the read end normally blocks until a writer opens the FIFO, and vice versa. Arrange startup so neither side waits forever:
consume_events <"$fifo" &
consumer_pid=$!
produce_events >"$fifo"
producer_status=$?
wait "$consumer_pid"
consumer_status=$?
Check both statuses. If a consumer exits early, the producer may receive SIGPIPE. If a producer never closes the FIFO, a consumer waiting for EOF will never finish. Multiple writers can interleave output beyond the system’s atomic pipe-write limit, so a FIFO is not automatically a record-safe message queue.
Use process substitution for one command graph
In Bash, Zsh, or Ksh, a command that insists on a filename can consume a stream with <(...):
diff <(sort left.txt) <(sort right.txt)
The shell supplies a /dev/fd path or manages a temporary FIFO. This is concise for processes launched within one shell command, but it is not POSIX sh syntax and its child statuses may require explicit handling.
Know when to choose another mechanism
FIFOs provide no durable queue, authentication, replay, schema, or backpressure policy beyond blocking writes. Use Unix-domain sockets for bidirectional local protocols, ordinary files for durable handoff, and a real message broker when delivery guarantees matter. Named pipes are excellent for bounded streaming—not a substitute for every IPC system.
Sources: