SyntaxStudy
Sign Up
Linux / Bash Beginner 1 min read

Pipes and Standard Streams

Every Linux process has three standard data streams: standard input (stdin, file descriptor 0), standard output (stdout, file descriptor 1), and standard error (stderr, file descriptor 2). By default, stdin reads from the keyboard and stdout/stderr write to the terminal. Shell redirection and pipes manipulate these streams to connect processes and files. The pipe operator `|` connects the stdout of one command to the stdin of the next, forming a pipeline. This is the heart of Unix composability: small, single-purpose tools wired together to perform complex transformations. Pipelines are evaluated left-to-right and can chain as many commands as needed. Each stage in the pipeline runs as a separate process, often in parallel. Redirection operators change where stdin and stdout connect. The `>` operator redirects stdout to a file, overwriting it. `>>` appends rather than overwrites. `<` redirects a file to stdin. These operators can be combined and can target specific file descriptors, giving precise control over where each data stream flows.
Example
# Basic pipe: count lines in a file
cat /etc/passwd | wc -l
# More efficient equivalent:
wc -l < /etc/passwd

# Multi-stage pipeline: top 5 largest files in /var
du -sh /var/* 2>/dev/null | sort -rh | head -5

# Redirect stdout to a file (overwrite)
ls -la > filelist.txt

# Redirect stdout to a file (append)
date >> timestamps.log

# Redirect stdin from a file
sort < unsorted.txt

# Redirect stderr to a file
find / -name "*.conf" 2>errors.txt

# Redirect both stdout and stderr to same file
command > output.txt 2>&1
# Or with Bash 4+ shorthand:
command &> output.txt

# Redirect stderr to /dev/null (suppress errors)
find / -name "*.log" 2>/dev/null

# Redirect stdout to one file, stderr to another
command > stdout.txt 2>stderr.txt

# Here string: pass a string as stdin
grep "root" <<< "root:x:0:0:root:/root:/bin/bash"

# Here document: multi-line stdin
cat <<EOF
Line 1
Line 2
Line 3
EOF

# Combine pipeline with redirection
grep "ERROR" /var/log/app.log | awk '{print $1}' | sort | uniq -c > error_report.txt