Tuesday, June 24, 2008

Redirecting stderr

I have some scripts that echo warning messages that really should go to stderr instead of stdout. The basic form is

echo "error" >&2

but I've twice gotten frustrated when I've tried to get fancier than that (not with echo, but with other commands). The essential problem is that the redirecting to stderr needs to be the last redirection. So my quick example is

echo "error" > /dev/null >&2

where redirecting to /dev/null or a file must come before the stderr part.

Hope this saves some time!

No comments:

Post a Comment