Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

A Unix pipe connects file descriptors of two processes. A pipe is created with the POSIX pipe() function declared in <unistd.h>. Shells provide pipe creation between processes using "|".

Pipes and data streams

Every command or program executed by the shell has 3 data streams associated with it:

  1. standard input (stdin, with a file descriptor of 0) – where commands get their input from (by default, keyboard input provided by the terminal).
  2. standard output (stdout, file descriptor 1) – where commands send their output (by default, the terminal display).
  3. standard error (stderr, file descriptor 2) – where commands send their error and warning messages (by default, the terminal display).

There are ways to connect streams between programs and files called piping and redirections.

Piping is a mechanism for sending data from one program to another using the "|" operator in most shells. The operator feeds the output from the program on the left as input to the program on the right.

Example:

$ cat two_columns
column1:cloth
column2:strawberries
column3:fish
column4:chocolate
column5:punch cards

$ cat two_columns | awk -F: '{print $1}'
column1
column2
column3
column4
column5

$ cat two_columns | awk -F: '{print "HAS: " $2}'
HAS: cloth
HAS: strawberries
HAS: fish
HAS: chocolate
HAS: punch cards

Further reading

Related tags

Useful links

history | excerpt history