Pipes & Redirection
Bash · Reference cheat sheet
Pipes & Redirection
Bash · Reference cheat sheet
📋 Overview
Redirection connects command stdin/stdout/stderr to files or other FDs. Pipes (|) connect stdout of one process to stdin of the next. Master these to build reliable pipelines and capture errors separately.
🔧 Core concepts
| Operator | Meaning |
|---|---|
> | Truncate/write stdout |
>> | Append stdout |
2> / 2>> | stderr |
&> / &>> | stdout+stderr (bash) |
> / 2>&1 | Merge stderr into stdout |
< | stdin from file |
<<EOF | Here-document |
<<<"$s" | Here-string |
| | Pipe stdout → next stdin |
| ` | &` |
n>&m | Dup FD n to m |
/dev/null | Discard |
FD 0 stdin, 1 stdout, 2 stderr. Order matters: cmd >out 2>&1 merges both into out; cmd 2>&1 >out leaves stderr on the terminal.
💡 Examples
Capture and merge:
make >build.log 2>&1
make 2>&1 | tee build.log
curl -fsS "$url" -o file || echo "failed" >&2Here-doc and here-string:
ssh host bash <<'EOF'
hostname
uptime
EOF
grep -F <<<"$needle" file.txtSwap / silence:
cmd 2>/dev/null
cmd >/dev/null 2>&1
cmd 3>&1 1>&2 2>&3 3>&- # swap stdout/stderr (classic trick)Process with pipefail:
set -o pipefail
curl -fsS "$url" | jq '.id'⚠️ Pitfalls
- Without
pipefail, pipeline status is the last command only—failures earlier are hidden. >overwrites silently; preferset -o noclobber(>|to force) for safety.cmd > file 2>&1vscmd 2>&1 > file—order changes which FD lands where.- Pipes introduce subshells; side effects (vars,
cd) don’t affect the parent. - Here-docs without quotes expand variables; use
<<'EOF'for literals. - Large pipes buffer; for huge data consider temp files or streaming tools.