Linux: Redirecting or Silencing Script Output

I've been writing shell scripts on *nix systems for more than twenty years. You'd think that by now I'd have this memorized, but I don't. Since I'm tired of looking it up every time, I'll post this here so that it can help jog my memory next time around.

Repeat after me: redirecting stderr and stdout is easy.

To run your script while silencing any and all possible output, execute it like this:
./ > /dev/null 2>&1

To silence it only when it runs via cron, to make it not send you an email after it runs, put it in your crontab like this:
0 5 * * 1 /home/aiverson/ > /dev/null 2>&1
(all on one line)

If you're running a command inside a script and you want to capture both possible errors and standard  output in the same way, so that you can parse it easily, do this:
OUTPUT=`lynx -dump "http://website" 2>&1`
If you don't add the 2>&1, any error message returned by lynx is sent to the active window running your script, instead of into the $OUTPUT variable.

No comments: