Home > Tutorials > Linux Server Tutorials > The Bash Shell: Pipelines

The Bash Shell: Pipelines

Pipelines are a great way to string together a series of commands. This means that the output from the first command in the pipeline is used as the input for the second command in the pipeline. The output from the second command in the pipeline is used as the input to the third command in the pipeline, and so on, and so on. The output from the last command in the pipeline is the only output that you will actually see displayed on the screen. You can tell bash to create a pipeline by typing two or more commands seperated by a vertical bar, or pipeline character (|). For example:

cat sample.text | grep "High" | wc -l

The above pipeline would take the output from the cat command (listing the contents of a file). and send them to the grep command. The grep command searches for each occurrence of the word High in it’s input. The grep command’s output would now consist of each line within the file that contained the word High. This output is now sent to the wc command where the wc command with the -l option prints the total number of lines contained in it’s input.  Let’s pretend the contents of our sample.text file are as follows:

Today's To-Do List:
Low: Go buy eggs
High: Return Late Movie
High: Pay Rent
Medium: Pick up dry cleaning

Now if we ran the above pipelined command on our sample.text file, our output would be “2” – indicating that we have 2 things of high importance to do today.

About Michael Boguslavskiy

Michael Boguslavskiy is a full-stack developer & online presence consultant based out of New York City. He's been offering freelance marketing & development services for over a decade. He currently manages Rapid Purple - and online webmaster resources center; and Media Explode - a full service marketing agency.

Check Also

How To Populate A DIV via an HTML SelectBox, PDO, and jQuery

Imagine you have a database full of information, and you want to let users browse ...

Like every other website, this site uses cookies to analyze our traffic. Cookies may also be utilized by our advertisers and partners. By using this website you agree to the use of said cookies. More Information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings, continuing to navigate past this message, or you click "Accept" below then you are consenting to the use of cookies on the Rapid Purple website.

Close