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

Check if input parameter exists in Bash

Publish Date:2025/03/21 Views:191 Category:OPERATING SYSTEM

When we create a Bash script, we may want to use parameters in our script to run successfully. Therefore, we need to create a script to check the number of input parameters used by the user in the script. All of this prevents unexpected beh

Breaking out of a loop in Bash

Publish Date:2025/03/21 Views:131 Category:OPERATING SYSTEM

Using loops is a common task in any programming or scripting language. While using a loop, sometimes we need to stop it under a predefined condition. Like other programming and scripting languages, Bash uses the keyword break to stop any lo

Bash configuration files in macOS

Publish Date:2025/03/21 Views:185 Category:OPERATING SYSTEM

In this article, we will discuss how to create, delete, and edit bash profiles in macOS. Create .bash_profile To create .bash_profile, open a terminal and move to your home directory using the following command. cd ~/ Now you can create usi

eval command in bash script

Publish Date:2025/03/21 Views:87 Category:OPERATING SYSTEM

This article is about using strings as commands in Bash scripts. For this purpose, the eval command is used. Eval Command in Bash Scripts In some Bash scripts, you have to create a string using variables or input values ​​(for example)

Exit Bash Script

Publish Date:2025/03/21 Views:99 Category:OPERATING SYSTEM

This article provides a brief introduction to Bash scripting and discusses exiting a Bash script when an error occurs. It further discusses the limitations and benefits of Bash scripting. What is Bash Scripting A computer script/program tel

timeout command in Bash

Publish Date:2025/03/21 Views:105 Category:OPERATING SYSTEM

This article is a simple guide to setting a timeout for a specific program using the timeout command from GNU's coreutils package in Bash. timeout command in Bash In many cases, such as fetching data from a server or running a function or p

Start a new terminal session in Bash

Publish Date:2025/03/21 Views:51 Category:OPERATING SYSTEM

In various situations when using Bash or another shell, you may need to run a script or program in a new terminal instance or in another tab in the same terminal. Opening a new terminal instance or tab from within Terminal is simple; we'll

Checking Syntax in Bash

Publish Date:2025/03/21 Views:89 Category:OPERATING SYSTEM

This article discusses methods for checking Bash scripts for syntax problems without running the script. Syntax errors are caused by some grammatical or spelling mistakes in the code. In modern compilers of other programming languages, the

Run batch (.bat) files in CMD

Publish Date:2025/03/20 Views:155 Category:OPERATING SYSTEM

This article will show you how to use CMD to run a batch file.bat. There are three ways to run a batch file. Let us discuss them in the following sections. Run batch (.bat) files in CMD by directly clicking on them This way you can just go

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial