← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Shell Pipes & Redirects

linux Intermediate

Also Known As

pipes stdin stdout stderr redirects

TL;DR

Pipes (|) connect stdout of one command to stdin of another; redirects (<, >, >>) send stdin/stdout to files — the foundation of composable Unix command-line workflows.

Explanation

Standard streams: stdin (0), stdout (1), stderr (2). Redirect stdout: cmd > file (overwrite), cmd >> file (append). Redirect stderr: cmd 2> error.log. Redirect both: cmd > out.log 2>&1. Pipe: cmd1 | cmd2 connects cmd1's stdout to cmd2's stdin. Named pipes (mkfifo) persist as filesystem objects. Pipes are synchronous — cmd1 runs concurrently with cmd2, with a kernel buffer between them. Understanding pipes enables composing tools: cat access.log | grep '500' | awk '{print $1}' | sort | uniq -c | sort -rn

Common Misconception

cmd > out.log 2>&1 and cmd 2>&1 > out.log are equivalent — order matters: the second form redirects stderr to the terminal stdout first, then redirects stdout to the file — stderr still goes to the terminal.

Why It Matters

Pipes and redirects are how PHP deployment scripts, log analysis, and server administration tasks are composed from simple tools — essential for writing correct bash scripts.

Common Mistakes

  • Losing stderr in deployment scripts — use 2>&1 to capture errors alongside stdout in logs.
  • 2>&1 > file vs > file 2>&1 — order matters; always put the file redirect first: > file 2>&1.
  • Using cat file | grep instead of grep file — unnecessary pipe; grep reads files directly.
  • Not quoting pipes in PHP exec() — shell special characters in arguments can break the pipe.

Code Examples

✗ Vulnerable
# Wrong order — stderr not captured in file:
deploy.sh 2>&1 > deploy.log
# stderr goes to terminal (original stdout at time of 2>&1)
# stdout goes to deploy.log
# Errors missing from log!

# PHP: exec without capturing stderr:
exec('composer install', $output); // Errors from composer lost
✓ Fixed
# Correct order — both streams to file:
deploy.sh > deploy.log 2>&1
# stdout → file, then stderr → same as stdout (now the file)

# PHP: capture stderr too:
exec('composer install 2>&1', $output, $exitCode);
if ($exitCode !== 0) {
    file_put_contents('/var/log/deploy_errors.log', implode("\n", $output));
}

Added 15 Mar 2026
Edited 22 Mar 2026
Views 22
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S 1 ping S 0 pings M 1 ping T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 1 ping T 2 pings F 0 pings S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T
No pings yet today
No pings yesterday
Amazonbot 6 Google 5 Perplexity 3 Unknown AI 3 Ahrefs 2
crawler 18 pre-tracking 1
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Low
⚡ Quick Fix
Understand pipes (|) and redirects (>, >>, 2>&1) to build PHP deployment scripts — grep errors in PHP logs, transform with awk, count with wc, all without writing PHP
📦 Applies To
bash cli
🔗 Prerequisites
🔍 Detection Hints
PHP script parsing log files that shell pipeline would handle; missing stderr redirect causing lost error messages; deployment script not capturing output
Auto-detectable: ✗ No bash grep awk sed
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: High ✗ Manual fix Fix: Low Context: File

✓ schema.org compliant