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?
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: