I have a bash script that I want to globally enable set -e.
But rather than disable it and reenable it all the time, I'm wondering if there is a way of selectively disabling error handling just sometimes. For example, commands run from systemd can be preceeded by a minus to ignore errors. Does bash has an equivalent?
e.g.
#!/bin/bash
set -e
WAN_IF=eth2
# Ignore error on next line
tc qdisc del dev ${WAN_IF} root
# I want errors to stop the script on this line
tc qdisc add dev ${WAN_IF} root handle 1: htb default 10
...
etc
Because of the need to enable/disable a lot I don't want to have to keep doing the following:
set +e
tc qdisc del dev ${WAN_IF} root
# I want errors to stop the script on this line
set -e
tc qdisc add dev ${WAN_IF} root handle 1: htb default 10
...
set -e, I would recommend this:old_set=$-; set +e; <code_returning_exit!=0>; set -$old_set. Just in caseset -eis not already set, you don't want to set it unnecessarily. functions are re-usable. So they may be called in a case whenset -eis not set. This precaution may not be required in a script, but in interactive shells, it may be relevant.set -eisn't that you have to be careful about which commands you want to allow to fail. The problem is that it doesn't compose. If someone else reads this post and usessource yourfile || :to source this script while allowing failure, suddenlyyourfilewill no longer stop on errors anywhere. Not even on failing lines after an explicitset -e.