0

This question already has an answer here:

Say I have something like this:

set -e;
echo 'doing some stuff'
false;
status=$?
post_hook send-message-elsewhere-with-status $status

One way to do what I want is:

set -e;
echo 'doing some stuff'
(
set +e
false;
status=$?
post_hook send-message-elsewhere-with-status $status
)

but is there some other way?

marked as duplicate by Kusalananda bash May 18 at 6:49

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

0

If there's a specific line to be made conditionally immune to set -e, this works:

false || true

This can be shortened to:

false || :

Or most cryptically:

false||:

If there's several lines, surround them in curly braces:

{ false; false; } || true

Note that false here represents a stand-in for any code that might return an error code. Of course false prints no output; but something like ls /bin/bash /bin/bbbbbbbash would print to STDOUT and STDERR, and using the above method it still would.

Or surround the code to be made immune with set statements:

set +e; false ; set -e

Note: the above methods work whether the lines in question return an error code or are successful. If it's known that a specific line is certain to return an error, all that's needed is a leading !:

! false
  • np, yeah the last line that always needs to is an after hook – user282164 May 18 at 17:04
1

Your question does not provide a lot of context to determine what it is you actually want to accomplish, but man bash is clear that with set -e your script will exit immediately when a non-zero exit status is received, Except if the failing command is part of:

  • the command list immediately following a while or until keyword;
  • the test following the if or elif reserved words;
  • any command executed in a && or || list except the command following the final && or ||;
  • any command in a pipeline but the last; or
  • if the command's return value is being inverted with the '!'.

So you have several option aside from a subshell to arrange your command as part of, to ensure the next line is executed if your command fails with a non-zero return

  • its basically for an after hook that always needs to run – user282164 May 18 at 17:04