Environment: GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin20)
I'm attempting to trap the exit from another function but then continue executing the program. In an object oriented language you could catch an exception and then continue execution without re-throwin; that is essentially what I'm trying to do. I'm expecting the function foo()
to exit
, but in this case I want to catch it and continue execution of the program.
#!/bin/bash
function doNotExitProgram()
{
echo "Ignoring EXIT"
# Magic happens here
}
trap doNotExitProgram EXIT
function foo()
{
echo "Inside foo()"
exit 170
}
foo
echo "Continue execution here"
Expected:
Inside foo()
Ignoring EXIT
Continue execution here
Actual:
Inside foo()
Ignoring EXIT
Steps tried so far:
Tried using
shopt -s extdebug
but that doesn't seem to work with EXIT.Tried
trap - EXIT
insidedoNotExitProgram()
Tried
trap - EXIT
returnreturn 0
insidedoNotExitProgram()
Tried
trap - EXIT
returnreturn 1
insidedoNotExitProgram()
Tried
return 0
insidedoNotExitProgram()
Tried
return 1
insidedoNotExitProgram()
Tried
trap "" EXIT
insidedoNotExitProgram()
This scenario is not described on Traps on tldp.org or on the trap man page.
EDIT: If possible do not change foo()