I was writing a script and then came across a odd problem. If I'd source a script that contains a bunch of functions that may call an error function which outputs a string and then exits, it will exit my shell. I know why it does it. It is because a function call is in the same process space as the caller (at least it is in bash), so the exit within the function terminates the current process with the exit code provided. Example:
error()
{
echo $1
exit 1
}
fn()
{
if [ $# == 0 ]; then
error "Insufficient parameters."
fi
# do stuff
}
$ fn
Insufficient parameters.
[shell terminates]
So my question is, can I exit all functions in the function stack without terminating the current shell and without spawning a new subshell?
Thanks