Pipes in Bash

Fumbani Banda Jan 10, 2022
  1. Pipes in Unix and Linux
  2. Using Pipes in Bash
  3. Example of Using Pipes in Linux
Pipes in Bash

This tutorial explains what pipes are and their usage in Unix and Linux environments.

Pipes in Unix and Linux

Pipes in Unix and Linux environments pass the standard output of one command as standard input to the other command for further processing. Pipes use the pipe operator to pass the output of a command to the other command as input. The pipe operator is a vertical bar, |.

When using pipes, data flows from the left to the right. Pipes help to seamlessly and continuously pass data between commands instead of using text files to pass data.

The following syntax is used for pipes.

command1 | command2 | command3

From the syntax above, the standard output of command1 is piped to command2 as standard input. command2 processes the standard input from command1 and pipes the output to command3 for further processing.

Using Pipes in Bash

The following examples demonstrate using pipes in Bash.

In the image below, the first ls command lists all the contents of the test folder. The second ls command pipes the standard output to the head command as standard input.

The head command has the -2 option; this tells the head command to display the first two lines from the output of the ls command.

pipe img1

The first cat command displays the file’s contents in the image below, file1.txt. The second cat command pipes the standard output to the grep command.

The grep command matches the line that has the pattern John in it, and once the line is found, the output is piped to the tee command, which writes the line to the output.txt file. The tee command also pipes its standard output to the wc command.

The wc command counts the number of words, characters, or lines in the file. The options of wc have been set to -mwl. The -m options tell the wc command to count the number of characters in the file and print it to the screen.

Use the -w option to count words in the file, and the -l option is used to count the number of lines in the file, and the figures are printed out to the standard output through the terminal.

The output.txt file has 1 line, 4 words, and 21 characters.

pipe img2

Example of Using Pipes in Linux

Below is the file.txt content we will use in this article.

first line
line 2
line 3
Hello
test line
Hello
World!

Pipe the cat to the wc

The cat command prints the file content. The wc -l command takes a file as input and prints the line number of the file to the screen. When we pipe the cat to the wc, the output of the cat is given as input to the wc instead of being written to the screen.

cat file.txt | wc -l

Cat pipe wc

Pipe the sort to the uniq

sort sorts and prints lines of text files. The uniq command takes a file as input, omits the repeated lines, and prints the remainder lines to the screen.

When we pipe the sort to the uniq, the output of the sort is given as input to uniq instead of being written to the screen.

sort file.txt | uniq

Sort pipe uniq

Fumbani Banda avatar Fumbani Banda avatar

Fumbani is a tech enthusiast. He enjoys writing on Linux and Python as well as contributing to open-source projects.

LinkedIn GitHub

Related Article - Bash Pipe