</>
Skip to content
Bash lessons (19/29)

Bash — Pipes

Basic pipes

#!/bin/bash

# Chain commands
ls -la | grep ".txt" | wc -l

# Count words in file
cat file.txt | wc -w

# Find large files
du -sh * | sort -rh | head -10

Pipe operators

#!/bin/bash

# Pipe
command1 | command2

# Pipe with error
command1 2>&1 | command2

# Tee (write to file and stdout)
echo "Hello" | tee file.txt
echo "World" | tee -a file.txt  # Append

Process substitution

#!/bin/bash

# Compare directory contents
diff <(ls dir1) <(ls dir2)

# Compare files
diff <(sort file1) <(sort file2)

Named pipes (FIFO)

#!/bin/bash

# Create named pipe
mkfifo /tmp/mypipe

# Writer (in one terminal)
echo "Hello" > /tmp/mypipe

# Reader (in another terminal)
cat /tmp/mypipe

Mini Practice

Write Bash code that:

  1. Chains commands with pipes
  2. Uses tee to write to file and stdout
  3. Uses process substitution for comparison
  4. Creates and uses a named pipe

Up Next

In the next lesson, you'll learn about Commands — essential Bash commands.

Related Topics

Frequently Asked Questions about Pipes

What is Pipes in Bash?

Pipes is a fundamental concept in Bash. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Pipes?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Pipes.

Why is Pipes important in Bash?

Pipes is essential for Bash development. Understanding this concept will help you write better code and solve real-world problems more effectively.