1. 3

  2. 2

    In the proposed solution, doing:

    if exit_code; then

    where exit_code is a variable is not going to work. That’ll try to run something called exit_code. You need something like (( ! exit_code )).

    For what it’s worth, zsh also has an errreturn option.

    1. 1

      Thanks for catching this! I will fix it.

    2. 1

      I’m a huge fan of the unofficial Bash strict mode. I use it for all my personal scripts as well as for other projects when I can.

      I just learned what set -e does


      this stuff isn’t new, it’s been working like this for several decades

      why advocate for things that you don’t understand

      1. 4

        This isn’t a very helpful comment, and it’s not in the spirit of this site.

        What specifically do you think that the author is misunderstanding?

      2. 1
        # Execute command and capture exit code, regardless of whether the command succeeded or not
        f && exit_code=$? || exit_code=$?

        Wouldn’t the following be better, or am I missing something?

        f; exit_code=$?
        # or
        1. 3

          This is a blog-post about “unofficial strict” mode, or set -e mode. In that mode, if you run

          f; exit_code=$?

          …then if f fails, bash will exit immediately, and will not run exit_code=$? or any other part of your script.

          1. 1

            Right! Thanks.