Bash Double Pipe

Numan Shafi Feb 15, 2024
  1. Pipes in Bash
  2. Difference Between Single Pipe and Double Pipe
  3. Conclusion
Bash Double Pipe

This article explores pipes and double pipes to a sufficient extent. It will discuss the usage, notations, and impacts of pipes in Bash command scripts.

In real life, pipes carry fluids or gas from one place to another. But in Bash, pipes do not carry the fluids but pass the output of one command as the input of the following command.

Pipes in Bash

A pipe is a redirection operator that transmits the command’s output to the input of the very following command. The | symbol is used as the pipe in Bash.

We can also say that in Linux/UNIX systems, a pipe connects the stdout of one command to the stdin of the other command.

Pipes are also used to combine two or more commands in which the output of the previous command becomes the input of the following command. This thing removes the overhead of file usage.

Usually, when we have to extract some related information from the file, we first save the previous output to the file and read from the file to convert the file stream to the required output.

However, in pipes, the output of one command directly feeds into the other command as an input. Below shows the basic notation of the command.

Comand-1 | command-2 | .... | command-N

Add Pipe Between ls and grep

Now we will see the example in which a pipe connects the output of one command to the input of the following command. In this example, we will pick the desired file by its name from a bunch of the files in a given directory.

Just open the terminal and go to the directory from where you have placed the file, and write the ls command on it. The ls -l command will list the details of the selected directory files.

However, our goal is to pick the output of ls -l, add a pipe, and search for a particular file name so that the output may only contain the required file.

Following the code, first list the name of all the files in a given directory.

ls -l

Output for ls command

The current directory has three files; their details are shown in the screenshot. However, we are interested in the details of the bash_double_pipe.md file.

We will use the pipe and grep that file with its name.

The following code will show the details of the bash_double_pipe.md.

ls -l | grep "bash_double_pipe.md"

ls with pipe command

Avoid I\O Redirection Using Pipes

Pipes do not use the standard input or out during the redirections. Pipes are implemented in the main memory.

The following example will show the stream redirection with and without the pipes.

I\O Redirection Without Pipe

ls -l > temp

more < temp

The above-given code redirects the output of the ls -l to the temp file and then shows the content of the temp file on the screen. The following screenshot shows the output of the above running code.

io redirection

Now, we will explore the IO redirection without using the pipe’s standard input or output. The above example redirects the first command’s output to the second command’s input.

The following code serves the same IO redirection operation but uses a pipe.

ls -l | more

The above code will perform the same redirection operation without disk IO. The following screenshot shows the results of the above running command.

io redirection with bash

The screenshot above shows that we can do redirections using the pipe without disk IO.

Use Pipe on cat, grep, tee and wc Commands

Pipes are also very useful in cat, grep, tee, and wc commands. Let us say we want to see how many times a record occurs in the file.

Then, instead of creating many variables, we need to use the pipe between these commands and get the required output.

The following code shows the usage of cat, grep, tee, and wc commands along with the pipes.

cat abc.txt | grep "temp" | tee file2.txt | wc -l

The cat command passes all the content of the abc.txt and passes to the grep command. The grep command only picks the rows with the matching string with the temp.

Then the rows containing the temp string are passed to the tee command, which writes the output to the file2.txt.

After that, this output is passed to the wc command, which counts the number of the record in it. The file2.txt has three rows; the output of the above command is 3.

cat grep pipe

Summing up the article, pipes are very useful for transferring the output of one command to the input of the following command.

Difference Between Single Pipe and Double Pipe

Single pipe | sends the output of one command to the input of the next command but double pipe || is not similar to the single pipe. Double pipe represents the OR operator and is generally used in conditional statements.

Syntax of OR Operator and Short-Circuit Evaluation

The following code shows the basic notation of the double pipe.

command-1 || command-2

The above code shows the basic notation and use of the || operator. Bash supports short-circuit evaluation.

The short-circuit evaluation means that the evaluation of a condition stops and returns the final decision soon after it becomes clear. For example, a compound condition having multiple expressions combined with the OR operator will stop as soon as one of the expressions turns out to be true.

It does not further evaluate other expressions, as their results will no more affect the final decision.

Therefore, the Bash interpreter tries to execute the first command in the Bash script above. If the first command executes successfully, then command-2 will not execute.

The second command will only execute if the first command does not execute successfully.

Let’s explore the example of the double pipe.

ls -a || echo "hello world"

In the above script, in command-1, we list the elements of the current directory. While in command-2, we print the message hello world.

We used the double pipe between these two commands. When we run the above command, command-1 successfully lists the elements of the current directory, and therefore, command-2 gets ignored.

Now let us explore the scenario in which command-1 is not executed successfully.

ls a || echo "hello world"

The above command shows the scenario in which the second command is executed. In the first command, we missed using a hyphen (-) before the a flag; therefore, this command will not run successfully.

So, the OR operator will execute the echo command, which prints the hello world message.

The following screenshot shows the output of the above two commands.

double pipe

The above idea can be extended to chaining any number of commands. For example, the following script will chain four commands with the || operator, and the evaluation will start from left to right.

Once any command executes successfully, all the remaining commands on the right shall get skipped.

Command-1 || Command-2 || Command-3 || command-4

Conclusion

This article discussed single and double-pipe signs and their functionalities. The single pipe symbol represents the Linux pipe, which puts one command’s output to another’s input.

On the other hand, the double pipe sign is a logical OR operator and is helpful in logical operations.

Related Article - Bash Pipe