🎓 A1Learner
🔍
0 of 45 lessons completed

Bash FIFO Pipelines

A Bash FIFO is a named pipe that passes data between commands through a filesystem name. A writer and a reader meet at the name, and data flows without a regular file.

A background writer and a foreground reader

The writer runs in the background, so the foreground cat can open the FIFO at once. The final line proves the writer was waited for.

mkfifo pipe.fifo
printf 'first line\nsecond line\n' > pipe.fifo &
cat pipe.fifo
wait
echo "fifo drained"

Opening both ends never blocks

Opening the FIFO for reading and writing at once supplies both sides in one step. The script writes and reads through the same descriptor.

mkfifo chan
exec 3<>chan
printf 'ping\n' >&3
IFS= read -r line <&3
echo "received: $line"
exec 3>&-
echo "fifo closed"

A reader loop drains the FIFO

The loop reads the FIFO until the writer closes it. Each handled line proves one item crossed the pipe.

mkfifo jobs.fifo
printf 'job one\njob two\n' > jobs.fifo &
while IFS= read -r job; do
  echo "handled: $job"
done < jobs.fifo
wait

Example

mkfifo orders.fifo
producer() { printf 'order a\norder b\norder c\n' > orders.fifo; }
producer &
count=0
while IFS= read -r order; do
  count=$((count + 1))
  echo "took: $order"
done < orders.fifo
wait
echo "orders taken: $count"
rm orders.fifo

Key points

  • mkfifo: the mkfifo command creates a FIFO at a path.
  • Blocking open: opening a FIFO for writing alone waits until a reader opens it, and the reverse is also true.
  • Background writer: starting the writer in the background lets the foreground reader open the FIFO at once.
  • Both ends: opening a FIFO for reading and writing at once never blocks, because the opener is both sides.
  • Once read, data is gone: each byte leaves the FIFO when the reader takes it, so a second reader gets nothing.
  • Cleanup: remove a FIFO when done, since it stays on disk like a file.

In this example, a producer function writes three orders into a FIFO from the background. The foreground loop reads the FIFO until the writer closes it. The count at the end proves all three orders arrived, and the FIFO is removed when done.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

Why does a script start a FIFO writer in the background before reading in the foreground?