anordal / shellharden

The corrective bash syntax highlighter
Mozilla Public License 2.0
4.63k stars 130 forks source link

Gotcha: Errexit is ignored depending on caller context #4

Closed kerolasa closed 6 years ago

kerolasa commented 6 years ago

FYI. New versions of bash has set -E that 'If set, the ERR trap is inherited by shell functions'. That should make gotcha more and less annoying. By that I mean it is easy to avoid 'set -e' in every single function, but one still may have to go and update whole bunch of old scripts to use -E rather than -e.

anordal commented 6 years ago

Oh nice! I'll read up on this later.

anordal commented 6 years ago

This works

set -E
mortal() {
    false
    echo 'Supposedly unreachable'
}
trap 'echo "Failed in $FUNCNAME."; exit 1' ERR

mortal

but not this

set -E
mortal() {
    false
    echo 'Supposedly unreachable'
}
trap 'echo "Failed in $FUNCNAME."; exit 1' ERR

if mortal; then
    true
fi

set -E suffers the same problem!