1

I want to do something like this in a shell script main.sh. Is it possible?

set -e

******some code

unset -e

******some other code.

set -e

1
  • Updated the question. Commented Jun 4, 2020 at 2:44

2 Answers 2

3

@Charles Duffy on another thread answered this. not exit a bash script when one of the sub-script fails

set +e undoes set -e. However, using set -e is a bad idea in general; better to use || exit on individual commands where you want a nonzero exit status to be fatal. (Skip past the parable to the exercises if you're in a hurry). – Charles Duffy

Sign up to request clarification or add additional context in comments.

Comments

1
some_code || { if test $? -eq 42; then # handle error fi } 

Using set -e is NOT a bad idea in general, as it is similar to the default behavior exception handling in C++ and many other programming languages. OTOH, the default behavior (set +e) may be quite harmful for large scripts because it actually encourage users to ignore the returned values without any caveats.

Since there is no other mechanism effectively distinguishing an error condition from the (merely) exit status which is safe to ignore, ignorance of errors would be more dangerous than it would look like, probably some engineering disaster. This is already sufficient verified in "normal" programming languages without exception handling, like C.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.