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
@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
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.