Connecting Commands Together with Pipes ( |
)
You’ve learned how to redirect each of the three standard file descriptors to various locations, and seen why that’s often useful. But what if, instead of just redirecting input and output to and from various files, you wanted to connect multiple programs together?
On the command line, you can use the pipe character (|
) to connect together the output of one program to the input of another program. This is an extremely powerful paradigm that is heavily used in Unix and Linux to create custom sorting, filtering, and processing commands.
echo "some text \n treasure found \n some more text" | grep treasure
If you paste this into your shell, you’ll see treasure found
printed out. Here’s what happened:
- The first command,
echo
, runs and produced the output you see between double quotes (the newline characters make this a 3-line string). - The pipe character streams that output (file descriptor 1) to the input...