vim: ft=help:tw=78:norl:nolist:nonu:noml:

set -o errexit  # :exit if a command exits with non-zero status
set -o nounset  # :exit if an expanded variable does not exist
set -o xtrace   # :like verbose mode -v, but expands commands
set -o noglob   # :filename globbing `set -f`
set -o pipefail # :if any command in pipeline fails, whole pipeline returns that code

The `set -e` option doesn't have effect in some situations, and this is the
standard behavior and portable across POSIX compliant shell.

---

The failed command is part of pipeline:

    false | true; echo printed

will print `printed`.

And only the failure of the pipeline itself is considered:

    true | false; echo 'not printed'

will print nothing.

---

The failed command run in the compound list following the `while`, `until`,
`if`, `elif` reserved word, a pipeline beginning with the `!` reserved word, or
any command as part of `&&` or `||` list except the last one:

    false || true; echo printed

The last command fails still make `set -e` affected:

    true && false; echo 'not printed'

---

The *subshell* fails in a compound command:

    (false; echo 'not printed') | cat -; echo printed


    cd foo &&
    make &&
    cat chicken >file &&
    [ -s file ] ||
    die "resulting file should have nonzero length"


Note: Of course you'll have to define the "die" function to do what you want,
but that's easy.

This is treating the `&&` and `||` (and even "|" if you want) like different
kinds of statement terminators instead of statement separators. So you don't
indent lines after the first one any further, because they're not really
related to the first line; the `&&` terminator is a statement flow control, not
a way to extend the statement. It's like terminating a statement with a `;` or
`&`. Each type of terminator has a different effect on program flow. See what I
mean?

It takes a little getting used to, but once you start writing like this, your
shell code starts getting a lot more readable. Before seeing this style, I
would tend to over-indent my code, which actually made it worse instead of
better.

By the way, take special note of the way we used the higher precedence of `&&`
vs `||` here. All the `&&` statements clump together, so that if any of them
fail, we fall back to the other side of the `||` and die.

Oh, as an added bonus, you can use this technique even if `set -e` is in
effect: capturing the return value using `&&` or `||` causes `set -e` to not
abort. So this works:

    set -e
    mv file1 file2 || true
    echo "we always run this line"

Even if the 'mv' command fails, the program doesn't abort.
Note: Because this technique is available, redo always runs all its scripts with
`set -e` active so it can be more like make. If you don't like it, you can
simply catch any "expected errors" as above.
