← Knowledge Base
BashReference note1 min read · updated 2026-11-27

Redirection and pipes

stdout, stderr, appending, discarding, tee and process substitution.

bashpipesredirection
cmd > out.txt            # stdout to a file (overwrite)
cmd >> out.txt           # append
cmd 2> err.txt           # stderr to a file
cmd > all.txt 2>&1       # both to the same file
cmd &> all.txt           # shorthand in Bash
cmd > /dev/null 2>&1     # discard everything
cmd | tee out.txt        # show AND save
cmd | tee -a out.txt     # append
sudo tee /etc/x.conf <<'EOF'
key=value
EOF
diff <(sort a.txt) <(sort b.txt)    # process substitution

sudo echo x > /root/file fails because the redirection runs as you; use echo x | sudo tee /root/file instead. Order matters: 2>&1 must come after > file.