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.

internal/shell.txt · Last modified: 2008/05/22 20:11 by solar
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate to DokuWiki Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki Powered by OpenVZ Powered by Openwall GNU/*/Linux Bookmark and Share