Bash Double Pipe
This article provides a thorough discussion of pipes and double pipes. It will discuss the usage, notation, and effects of pipes in Bash command scripts.
In real life, pipes carry fluids or gases from one place to another. But in Bash, pipes do not carry fluids, but rather pass the output of one command as input to the next.
Pipes in Bash
A pipe is a redirection operator that transfers the output of a command to the input of the command that immediately follows it. |
The symbol is used as a pipe in Bash.
We can also say that in Linux/UNIX systems, a pipe connects the standard output of one command to the standard input of another command.
Pipeline is also used to combine two or more commands, where the output of the previous command becomes the input of the next command. This thing eliminates the overhead of file usage.
Usually, when we want to extract some relevant information from a file, we first save the previous output to a file, and then read from the file to convert the file stream into the required output.
However, in a pipeline, the output of one command is directly given as input to another command. The basic notation of commands is shown below.
Comand-1 | command-2 | .... | command-N
Add a pipe between ls and grep
Now we will see an example of piping to connect the output of one command to the input of the next command. In this example, we will select the required file by name from a bunch of files in a given directory.
Just open the terminal and go to the directory where you have placed the files and write ls command on it. The ls -l command will list the details of the selected directory files.
However, our goal is to select the output of ls -l, add a pipe, and search for specific file names so that the output may contain only the desired files.
Following the code, first list the names of all the files in a given directory.
ls -l
There are three files under the current directory; their details are shown in the screenshot. However, we are interested in the details of the bash_double_pipe.md file.
We will use a pipe and grep the file and its name.
The following code will display the details of bash_double_pipe.md.
ls -l | grep "bash_double_pipe.md"
Avoid using pipes for I\O redirection
Pipes do not use standard input or output during redirection. Pipes are implemented in main memory.
The following examples show stream redirection with and without pipes.
I\O redirection without pipes
ls -l > temp
more < temp
The code given above redirects the output of ls -l to a temporary file and then displays the contents of the temporary file on the screen. The following screenshot shows the output of running the above code.
Now, we will explore IO redirection of standard input or output without using pipes. The above example redirects the output of the first command to the input of the second command.
The following code provides the same IO redirection operation, but uses pipes.
ls -l | more
The above code will perform the same redirection operation without disk IO. The following screenshot shows the result of running the above command.
The screenshot above shows that we can use pipes for redirection without disk IO.
Using pipes with cat, grep, tee, and wc commands
Pipes are also very useful in the cat
, grep
, , tee
and wc
commands. Suppose we want to see how many times a record appears in a file.
Then, instead of creating many variables, we need to use pipes between these commands and get the desired output.
The following code shows the use of cat, grep, tee, and wc commands and pipes.
cat abc.txt | grep "temp" | tee file2.txt | wc -l
cat
The command passes all the contents of abc.txt to the grep command. The grep command selects only the lines that have a string that matches temp.
The line containing the temporary string is then passed to the tee command, which writes the output to file2.txt.
This output is then passed to the wc command, which counts the number of records in it. file2.txt has three lines; the output of the above command is 3.
To summarize this article, pipes are very useful for transferring the output of one command to the input of the next command.
The difference between single pipe and double pipe
A single pipe |
sends the output of one command to the input of the next command, but a double pipe ||
is different from a single pipe. The double vertical bar represents the OR operator, which is generally used in conditional statements.
Syntax and short-circuit evaluation of the OR operator
The following code shows the basic notation for a double pipe.
command-1 || command-2
The above code shows ||
the basic notation and usage of the operator. Bash supports short-circuit evaluation.
Short-circuit evaluation means that the evaluation of a condition stops and returns a final decision as soon as it becomes unambiguous. For example, a compound condition with multiple expressions combined with the OR operator will stop as soon as one of the expressions evaluates to true.
It does not evaluate other expressions further, since their results no longer affect the final decision.
Therefore, the Bash interpreter will try to execute the first command in the above Bash script. If the first command is executed successfully, command-2 will not be executed.
The second command will be executed only if the first command does not execute successfully.
Let's explore the dual-pipe example.
ls -a || echo "hello world"
In the above script, in command-1, we list the elements of the current directory. In command-2, we print the message hello world.
We have used a 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 is ignored.
Now let us consider the scenario where command-1 does not execute successfully.
ls a || echo "hello world"
The above command shows the scenario of executing the second command. In the first command, we did not use a hyphen (-) before the a flag; therefore, the command will not run successfully.
Therefore, the OR operator will execute the echo command, printing the hello world message.
The following screenshot shows the output of the above two commands.
The idea above can be extended to chain any number of commands. For example, the following script will ||
chain four commands using the operator, and the evaluation will start from left to right.
Once any command is successfully executed, all remaining commands on the right will be skipped.
Command-1 || Command-2 || Command-3 || command-4
Summarize
This article discusses single and double pipe symbols and their functions. The single pipe symbol represents a Linux pipe, which puts the output of one command into the input of another command.
On the other hand, the double vertical bar symbol is the logical OR operator which facilitates logical operations.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
How to decompress x.tar.xz format files under Linux
Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM
-
A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr
Summary of vim common commands
Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM
-
In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme
Detailed explanation of command return value $? in Linux
Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM
-
? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re
Common judgment formulas for Linux script shell
Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM
-
In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –
Shell script programming practice - specify a directory to delete files
Publish Date:2025/04/08 Views:98 Category:OPERATING SYSTEM
-
Usually, in Linux system we need to frequently delete some temporary files or junk files. If we delete them one by one manually, it will be quite troublesome. I have also been learning shell script programming recently, so I tried to write
Use of Linux command at - set time to execute command only once
Publish Date:2025/04/08 Views:158 Category:OPERATING SYSTEM
-
This article mainly involves a knowledge point, which is the atd service. Similar to this service is the crond service. The functions of these two services can be similar to the two functional functions of javascript. Those who have learned
Use of Linux command crontab - loop execution of set commands
Publish Date:2025/04/08 Views:170 Category:OPERATING SYSTEM
-
Compared with at , which executes a command only once, crontab, which we are going to talk about in this article, executes the set commands in a loop. Similarly, the use of crontab requires the support of the crond service. The service is s
Linux practice - regularly delete files under the directory
Publish Date:2025/04/08 Views:198 Category:OPERATING SYSTEM
-
Since we want to delete the files under the directory regularly, we need to use the Linux crontab command. And the content format of each work routine is also introduced in the format of each crontab work. Similarly, we need to use shell sc
How to use the Linux file remote copy command scp
Publish Date:2025/04/08 Views:151 Category:OPERATING SYSTEM
-
Scp copies files between two hosts over the network, and the data is encrypted during transmission. Its underlying layer uses ssh for data transmission. And it has the same authentication mechanism and the same security level as ssh. When u