Take the 2-minute tour ×

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

1 Answer 1

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


