What is the difference between `|` and `||` in Linux?

QuestionsQuestions8 SkillsProDec, 20 2025
0144

In Linux, while these two symbols look similar, they serve completely different purposes. Think of the single pipe as a connector and the double pipe as a decision maker.

Here is the breakdown:

1. The Single Pipe (|) - "The Connector"

The pipe is used to pass data from one command to another.

  • Meaning: Takes the output of the first command and uses it as the input for the second.
  • Key usage: Combining tools to process data.
  • Example:
    ls -l | cowsay
    All the file details from ls -l are sent into the cow's mouth.

2. The Double Pipe (||) - "The Backup Plan"

The double pipe is a logical operator used for conditional execution.

  • Meaning: Run the second command ONLY IF the first command fails (returns an error).
  • Key usage: Handling errors or providing a fallback.
  • Example:
    cd folder_that_doesnt_exist || echo "Directory not found!"
    Since the cd command will fail, the echo command will run to tell you there was an error.

Comparison Table

Symbol Name Logic Analogy
` ` Pipe Flow data from A to B
` ` OR

One more for your toolkit: &&

To complete the set, Linux also uses && (Double Ampersand).

  • This means "Run the second command ONLY IF the first one succeeds."
  • Example: make && echo "Build successful!"

In summary:

  • Use | when you want to process data.
  • Use || when you want to check for errors.

0 Comments

no data
Be the first to share your comment!