1

At first, we can use set -e to exit the shell script immediately if any command failed(exit with non-zero code).

But, how can I exit it immediately in a conditional statement? For example:

set -xe

_if_error() {
  'run a bad command' # should exit immediately for this command does not exist
  echo -e "\033[31mUnexpected execution\033[0m"
}

if ! _if_error; then
  echo 'ok'
else
  echo 'bad'
fi

I hope the script echo ok, and do not echo red Unexpected execution.

acrazing
  • 1,955
  • 16
  • 24

0 Answers0