Search code examples
bashunix

How to disable set -e and set -o


So I have this at the start of a bash script file (-e and -o). However, in some functions, I would like for it to not exit out. Example

set -e
set -o pipefail

function check_status {
    echo "Start Check"
    docker exec mservice bash -c "echo 'Hello' | grep 'fail'"
    echo "End check"
}

check_status

How can I prevent this from exiting out of the script - basically if I run this, it would printout "Start Check", but then exit because the next command returns a '1'.

I would like to be able to disable and enable the set -e/-o in multiple places or in different functions.


Solution

  • For all options, the opposite of set -x is set +x (note the plus sign).

    So set +e will undo set -e, and set +o pipefail will undo set -o pipefail.