In shell scripts, set -e often used to make them more reliable by stopping the script when some of the commands executed from the script exit with a non-zero exit code.
It is usually easy to indicate that you do not need some of the following commands by adding || true || true to the end.
The problem arises when you really care about the return value, but don't want the script to stop at a non-zero return code, for example:
output=$(possibly-failing-command) if [ 0 == $? -a -n "$output" ]; then ... else ... fi
Here we want to check the exit code (thus, we cannot use || true inside the command substitution expression) and get the output. However, if a command in command substitution does not work, the entire script stops due to set -e .
Is there a clean way to prevent the script from stopping here without dropping -e and after that setting it back?
source share