Script Valley
Linux & Bash for Developers
Text Processing & SearchingLesson 2.3

How to use pipes and redirection in Bash

pipe operator, stdin stdout stderr, >, >>, 2>, 2>&1, /dev/null, tee command, command chaining

The Pipe: Unix's Most Powerful Idea

Every command in Linux reads from stdin and writes to stdout. The pipe | connects the stdout of one command directly to the stdin of the next. This lets you chain simple tools into complex processing pipelines without writing intermediate files.

Pipes in Practice

# Count how many processes are running
ps aux | wc -l

# Find the 5 largest files in current directory
du -sh * | sort -rh | head -5

# See only unique error messages in a log
grep "ERROR" app.log | sort | uniq -c | sort -rn

Redirection Operators

> redirects stdout to a file, overwriting it. >> appends instead of overwriting. 2> redirects stderr. 2>&1 merges stderr into stdout โ€” useful when you want all output in one log file.

# Save command output to file
ls -lah > directory_listing.txt

# Append to existing file
echo "Build completed" >> build.log

# Discard all errors (silence stderr)
command 2> /dev/null

# Save both stdout and stderr
build_script.sh > build.log 2>&1

# Write to file AND see it on screen
build_script.sh | tee build.log

tee splits output โ€” it writes to stdout (your terminal) and to a file simultaneously. Invaluable when you want to watch and record output at the same time.

Up next

sed command for find and replace in Linux

Sign in to track progress

How to use pipes and redirection in Bash โ€” Text Processing & Searching โ€” Linux & Bash for Developers โ€” Script Valley โ€” Script Valley