What's the best way to suppress output (stdout and stderr) unless the program exits with a non-zero code? I'm thinking:

  if ! "$@" > "$file" 2>&1; then
    cat "$file"
  rm -f "$file"

And run quiet_success my_long_noisy_script.sh but I'm not sure if there's a better way. I feel like this has to be something other people have needed to do.

For context, I'm looking to add this to my cron scripts so that I get emailed with everything if they fail, but not if they don't.

  • This should be the default behavior of most commands (no output on success). If not, the first thing to do is to look for an option / switch to enable such behavior. Failing that, your approach is the right idea. Side note: I'm assuming you posted pseudo-code because it is not actually valid sh syntax, and your redirection order is backwards (do > "$file" 2>&1 and use more quotes).
    – jw013
    Commented Jun 22, 2012 at 14:50
  • Yep, I just typed it up in the question. Applied your suggestion, and I agree, the command should be responsible for that, but alas...
    – dimo414
    Commented Jun 22, 2012 at 14:53
  • Just a syntax note: no need for parenthesis around the command.
    – manatwork
    Commented Jun 22, 2012 at 16:27

2 Answers 2


You're going to have to buffer the output somewhere no matter what, since you need to wait for the exit code to know what to do. Something like this is probably easiest:

$ output=`my_long_noisy_script.sh 2>&1` || echo $output
  • Note that if you use set -o xtrace in your shell script then all the output will be there again as part of logging the details of the assignment output=... :-). In that case it's probably better to use chronic. Commented Dec 5, 2019 at 19:30

The moreutils package contains a program chronic for this purpose. You just call it like

chronic my_program args ...

Very handy in cron jobs.

  • 4
    Do not be like me and mistake chronic for cronic, which is a similar program that suppresses output unless a command exists with a non-zero code or produces standard error output.
    – Witiko
    Commented May 6, 2018 at 16:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .