I use set -e
in scripts quite often, but sometimes, it's ok for a script to fail in some of it's commands.
I tried checking if set -e
was ON, but couldn't find a way to do it - there's no option (that I know of) in set
that will provide the current value.
Also tried this:
set | grep errexit
(errexit
is the option name for -e
) and set | grep "-e"
, no luck there.
I would like to check if the errexit
option is set, so I could temporarily disable it in some of my library functions that I built over time (using set +e
and re-applying set -e
if needed).
Something like:
if [ errexit is set ]; then
ERR_EXIT=1
set +e
fi
...
run some code that may fail
...
if [ "${ERR_EXIT}" = 1 ]; then
set -e
fi
cmd || true
to stop the script from exiting ifcmd
fails, or even better: you can makecmd
the subject of anif
statement that deals with its failure.local restore_errexit="$(shopt -po errexit)"
. The command outputs the code needed to set it back to what it is now. To restore, all you have to do is just run${restore_errexit}
. To just save and restore, it's the most simple way.