Output & Redirection in Bash
From the Bash Scripting cheat sheet · Input & Output · verified Jul 2026
Output & Redirection
echo, printf, and streams.
bash
# Quick Reference
echo "text"
printf "%s = %d\n" "x" 42
cmd > out.txt # stdout to file (overwrite)
cmd >> out.txt # append
cmd 2> err.txt # stderr to file💡 Prefer printf over echo for formatting and portability - echo flags vary across shells.
⚡ > truncates, >> appends; 2> redirects stderr; 2>&1 merges stderr into stdout.
📌 Order matters: cmd > f 2>&1 works; cmd 2>&1 > f does NOT merge as expected.
🟢 &>/dev/null silences a command completely (both stdout and stderr).
outputredirection