Piping Between Commands
The pipe metacharacter, |
, serves as a conduit, channeling the output of one command directly into the awaiting input of another. This seamless relay enables a succession of commands to collaboratively process data. Confused? Don't be - it's actually really easy.
Let's see an example that stitches together commands using pipes:
This command:
Prints the contents of the
/etc/passwd
file to the terminalPipes the output to the
sort
command, which arranges the entries alphabetically based on the usernames leading each linePipes the output to the
less
command, making it easier to browse through and navigate the output
Pipes perfectly embody the foundational ethos of UNIX, from which Linux evolved. At its heart, UNIX was envisaged as an operating system of interconnected building blocks. The principle was to cleverly chain these utilities together to handle a multitude of tasks.
Casting our minds back to the days before graphical word processors (sadly yes, I can remember those...), users penned plain-text files peppered with macros for formatting cues. To catch a preview of the document's actual appearance, they'd conjure up commands similar to:
Let's look more closely at what is happening here:
The contents of the
grep
man page (grep.1.gz
) are handed over togunzip
for decompressionnroff
steps in to format the page using the manual macro (hence,-man
)For a tidy presentation, we send it through
less
Given it's all in plain text, there's a bunch of flexibility here. You could sort the content, adjust parts, or even integrate text from another document.
A few points to zone in on...
man pages are like user manuals for computer commands, explaining what they do and how to use them
We asked
gunzip
to unpack thegrep.1.gz
file using a method called redirection. Essentially, we fed the file mentioned after the<
symbol intogunzip
. We'll dive deeper into this concept a bit laterNote that we used two pipes (
|
) - practically speaking, you can chain together as many commands using pipes as you want, or at least until you hit system limits like memory or CPU constraints. Just beware of complexity...
The standout point? Instead of relying on a single, hefty program, we achieve our goal by seamlessly chaining commands together, with piping and redirection being the stars of the show.
Last updated