Thursday, 5 April 2012

UNIX pipes: male | female > baby

What happens when you execute the following command on a Unix system?
male | female > baby
It will start two processes (in this case male and female) pre-supposing you have them in one of your bin or sbin folders.

Unix pipes (called from shell with the "|" character) reroute the output of the process on the left to the input of the process on the right. In this case, whatever male outputs to stdout, female will receive in stdin.

What happens to the output of female?

If we would have called this line:
male | female
the stdout of the last process (in this case female) will be displayed on your screen.

However, the ">" character allows you to capture the output of the last process to a file. In the first case, a file called baby.

Disclaimer: I realize this would make a cool line for the birth announcement, but it isn't one ;)