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:
All the file details fromls -l | cowsayls -lare 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:
Since thecd folder_that_doesnt_exist || echo "Directory not found!"cdcommand will fail, theechocommand 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.