How to untrap after a trap command

To unset all trapped signals, you can run "trap - signal" in a loop:

trap | awk '{ print $NF }' | while read SIG ; do trap - $SIG ; done

To ignore the failure of a command that you know may fail (but don't necessarily need), you can cause the line to always succeed by appending || true.

Example:

#!/bin/bash

set -e

failed() {
    echo "Trapped Failure"
}
trap failed ERR

echo "Beginning experiment"
false || true
echo "Proceeding to Normal Exit"

Results

Beginning experiment
Proceeding to Normal Exit

You can use this trap to reset trap set earlier:

trap '' ERR

Here is what you can find in the trap manual:

The KornShell uses an ERR trap that is triggered whenever set -e would cause an exit.

That means it is not triggered by set -e, but is executed in the same conditions. Adding set -e to a trap on ERR would make your script exit after executing the trap.

To remove a trap, use:

trap - [signal]

Tags:

Bash