With set -e, is it possible to ignore errors for certain commands?

I have a bash script that I want to globally include set -e.

But instead of disabling it and reusing it all the time, I wonder if there is a way to selectively disable error handling only occasionally. For example, commands run from systemd can be overridden by a minus to ignore errors. Does bash have an equivalent?

eg.

#!/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

Due to the need to enable / disable a lot, I do not want to continue to do 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
...
+4
source share
1 answer

Add || :(or anything else that is guaranteed not to work, but the simplest) to the end of the command.

, set -e , , ( ), - .

( , , , .)

, set -e:

set -e , , . , . - source yourfile || : script, , yourfile . set -e.

+4

All Articles