JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Using Double and Single Pipes in Bash

Author:JIYIK Last Updated:2025/03/21 Views:

In Bash, the double pipe || is also known as the OR operator in other programming languages. On the other hand, the single pipe | is known as a pipeline.

In this article, we will see how to use double pipe (also known as OR) and pipelines in Bash scripting. Also, we will see necessary examples and explanations to make the topic easier to understand.


Using double pipes in Bash ||

Putting this double pipe ||between two commands will try to run the first command. If running the first command is unsuccessful, it will execute the second command.

Note that if the first command runs successfully, the second command will not be run.

The general syntax for a double pipe is:

<COMMAND_1> || <COMMAND_2>

In the example shared below, we put two commands together with an OR operator. The code for our example looks like the following:

ls -l || echo "This is a text"

The output of the example shared below shows that it executed only the first command. The output of the above example will look similar to the following:

total 12
-rwxrwxrwx 1 author author   99 Aug  4 14:45 1_Test.txt
-rwxrwxrwx 1 author author  204 Jul 19 15:04 BatchCode.bat
-rwxrwxrwx 1 author author  159 Aug  5 23:04 example.sh
-rwxrwxrwx 1 author author   32 Jul 12 14:05 file1.bat
-rwxrwxrwx 1 author author   33 Jul 12 14:05 file2.bat
-rwxrwxrwx 1 author author   32 Jul 12 14:05 file3.bat
-rwxrwxrwx 1 author author 1330 May 25 00:54 sample.vbs

Using Single Pipe in Bash |

This |is also called piping in Bash. It is used when the output of the first command is used as the input of the second command.

The general syntax of a pipeline is:

<COMMAND_1> | <COMMAND_2>

Let's look at the following example. Suppose we have a Bash script with the following content:

echo "This is the text from the Bash script"

The following example will run the Bash script shared above using an external command. The entire command will look like this:

echo "This is a command" | ./example.sh

Now, after running the above command, you will get the following output:

This is the text from the Bash script

Please note that all the code used in this article is written in Bash. It can only be run in the Linux Shell environment.

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.

Article URL:

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 –

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

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial