Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to run a series of commands inside part of a script, and stop if any return a non-zero exit status. But also, inside the same script, I'd like a final command to always run, regardless of whether anything failed before.

#!/usr/bin/env bash
set -e
  # various commands go here
) &> /var/log/logfile
final_command_that_always_runs /var/log/logfile

Is this possible?

Perhaps I can 'set -e' for only one () section of a script?

share|improve this question
up vote 2 down vote accepted

Parens invoke a subshell, so yes, you can set options within and they won't affect the parent shell.

Edit from question asker: I've marked this as the answer, but I thought it needed a demo - as the answer says, I needed to move the set -e inside the parens:

#!/usr/bin/env bash
    set -e
    echo 'one'
    ls -la /var/notexist/two
    echo 'three'
) &> /var/tmp/logfile
cp /var/tmp/logfile /var/tmp/logfile2
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.