A pipe bridges two processes together. The pipe, written with the | character, connects the output of one command to the input of the second. It’s one of the fundamental building blocks of a Unix shell.
A very basic example is using cat to feed the contents of a file to the word count tool wc:
cat <filename> | wcCode language: plaintext (plaintext)
Just in case you didn’t know these commands:
catprints the contents of a file.wccounts all the lines, words, and bytes it is fed.
Another often-used version is wc -l, which only counts the number of lines in a file.
Let’s try this. First, create a file called words.txt with this content:
hello
world
python
ninja
are
you
?
3
2
1Code language: plaintext (plaintext)
Then run this command:
cat words.txt | wc -lCode language: plaintext (plaintext)
The output should be 10 since there are 10 lines in the file. Another example is using sort to sort the words in your file:
$ cat words.txt | sort
?
are
hello
ninja
python
world
you
1
2
3Code language: plaintext (plaintext)
As we can see, all the words from words.txt are fed to sort, which sorts them. Punctuation first, then numbers, and then letters.
Alright. Here’s one last questionable example of using pipes. There’s this awesome tool called cowsay. You probably need to install it since it’s likely not installed by default. It generates a text-based representation (also called ASCI art) of a cow saying any that you feed it:
$ echo "Are we going to learn anything useful?" | cowsay
________________________________________
< Are we going to learn anything useful? >
----------------------------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||Code language: plaintext (plaintext)
This is the stuff that will make a lasting impression on your colleagues.


