Shell tricks

How to redirect stderr but not stdout to a pipe

(taken from csh-whynot)

exec 3>&1; grep yyy xxx 2>&1 1>&3 3>&- | sed s/file/foobar/ 1>&2 3>&-
grep: xxx: No such foobar or directory

grep's normal output (on stdout) will be unaffected. We're closing fd 3 in case a program actually cares about this fd (although most programs don't). We send stderr to sed's stdin, and then put sed's stdout “back” to stderr.