Connecting Commands with a Pipe

A pipe can connect one process's ordinary output to another process's input. Each end sees a stream under its own interface.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace a simple transformation
  2. Preserve error reporting
  3. Follow bytes through the intended channels
  4. Test consumer exits and relevant failure propagation

Trace a simple transformation

One command writes three lines and another counts them. The count command can read the lines before all output is retained in a file. Standard input explains why the consumer need not choose the producer's origin.

Preserve error reporting

A pipe does not automatically combine all error statuses into one useful result. Check the actual shell and pipeline conventions. Exit status remains separate from arriving bytes.[1]

Follow bytes through the intended channels

A producer writes lines to standard output. A pipe connects them to a consumer's standard input. The producer may still write status messages to standard error, which normally travels elsewhere. The consumer can stop reading before the producer reaches the end. See standard output and error.

Producer stdout → pipe → consumer stdin
Producer stderr → independent destination
Combining both channels changes the data the consumer receives

Test consumer exits and relevant failure propagation

A downstream program can close its end early, which can affect the writer. Determine which output and exit states are meaningful. Test a failing producer and an early-exit consumer separately before using the pipeline in automation.

References

  1. The Open Group, POSIX Shell Command Language ↩