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
To exit the function stack without exiting shell one can use the command:
kill -INT $$
As pizza stated, this is like pressing Ctrl-C, which will stop the current script from running and drop you down to the command prompt.
Note: the only reason I didn't select pizza's answer is because this was buried in his/her answer and not answered directly.