With the pipe notation, we can pass the output of one program to another as its initial data.

Pipe Notation (|)

A Scenario-based Question

Let’s assume there’s a file called fresh_file that holds two lines of text:

1
cat fresh_file
img

Print fresh_file content

This file has only two lines of text, so the number of lines is noticeable, but what if you encounter a text file with a lot of content? How can we quickly and efficiently get the number of lines of a text file?

Count Number of Lines of the File Contents

We can pass the output of the cat command to the wc command through the pipeline feature of the shell to treat it as the initial data of the wc command:

1
cat fresh_file | wc -l
img

Pass the printed output to wc command to count the number of lines of the file content

Count Number of Words of the File Contents

1
cat fresh_file | wc -w
img

Pass the printed output to wc command to count the number of words of the file content

Count Number of Characters of the File Contents

1
cat fresh_file | wc -c
img

Pass the printed output to wc command to count the number of characters of the file content

Continuous Pipes

Bash supports multiple pipes run back-to-back:

1
cat fresh_file | wc -c | wc -l
img

The pipeline will execute these commands from left to the right one by one. Every subsequent command must be able to receive the results of the previous command

References 3.2.2 Pipelines

If you think the content of this article has helped you, and if you would like some more high-quality materials in the near future, please give me some modest support.

Buy me a coffeeBuy me a coffee