Alexander Mills
Alexander Mills

Reputation: 100000

Determine if `set -e` flag if is active in bash

In a bash script/shell, is there a programmatic way to determine if the set -e flag is active?

I just need a boolean letting me know if it's on/off.

Upvotes: 4

Views: 802

Answers (4)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798556

From help test:

    -o OPTION      True if the shell option OPTION is enabled.

Thus:

[ -o errexit ]

Upvotes: 7

whoan
whoan

Reputation: 8521

You can check the $- variable to see whether the e option is enabled:

[[ $- =~ e ]]

From help set:

The current set of flags may be found in $-.

Upvotes: 2

PesaThe
PesaThe

Reputation: 7499

You can also use the exit code of shopt:

if shopt -qo errexit; then 
    echo enabled
    # do something
fi

Upvotes: 4

George Vasiliou
George Vasiliou

Reputation: 6335

$ set -e
$ if grep -q 'errexit' <<<"$SHELLOPTS";then echo "set -e is enabled";else echo "set -e is disabled";fi
set -e is enabled
$ set +e
$ if grep -q 'errexit' <<<"$SHELLOPTS";then echo "set -e is enabled";else echo "set -e is disabled";fi
set -e is disabled

Upvotes: 2

Related Questions