7 May 2016
The Pipeline is one of the defining features of the Unix shell, and obviously, the same goes for Linux, MacOSX, and any other Unix-based or inspired systems.
In a nutshell, it allows you to tie the output of one program to the input of another. That may not seem such a radical idea, but the immediate benefit that it brings is that you don't have to save the results of one program before you can start processing it with another. The longer-term, and even more important benefit, is that it encourages programs to be small, simple, and to do one thing well. There is no need for every program to include a word-counter, if they can all be piped into wc
. Similarly, no program needs to offer its own built-in pattern matching facilities, as it can be piped into grep
. These simple concepts are what has made the Unix approach still massively important in today's world, more than 40 years after its invention.
This is another example of the Unix concept that "everything is a file". The output from one command is put into the pipeline, and the receiving command treats it just as it treats input from a file. If you run "ls | grep foo
", the result is identical to if you ran "ls > /tmp/my-temp-file
" followed by "grep foo /tmp/my-temp-file
" (and, of course, remembered to "rm /tmp/my-temp-file
" afterwards).
Anyway, over to Brian Kernighan to explain it all:
Learn another neat trick about pipelines with the pipestatus tip.
My Shell Scripting books, available in Paperback and eBook formats. This tutorial is more of a general introduction to Shell Scripting, the longer Shell Scripting: Expert Recipes for Linux, Bash and more book covers every aspect of Bash in detail.
![]() Shell Scripting Tutorial is this tutorial, in 88-page Paperback and eBook formats. Convenient to read on the go, and in paperback format good to keep by your desk as an ever-present companion. Also available in PDF form from Gumroad:Get this tutorial as a PDF | ![]() Shell Scripting: Expert Recipes for Linux, Bash and more is my 564-page book on Shell Scripting. The first half covers all of the features of the shell in every detail; the second half has real-world shell scripts, organised by topic, along with detailed discussion of each script. |