Capture exit status from function in shell script
You need to use return
instead of exit
inside the function:
_EXECUTE_METHOD () { return 1; }
_EXECUTE_METHOD || echo "Got error"
exit
will terminate your current shell. If you have to use exit
then put this function in a script or sub shell like this:
declare -fx _EXECUTE_METHOD
_EXECUTE_METHOD () { exit 1; }
_EXECUTE_METHOD || echo "Got error"
(..)
will execute the function in a sub-shell hence exit
will only terminate the sub-shell.