exit function stack without exiting shell
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.
you can do a
exit() { return $1;}
then
source ./your_script
In answer to the skeptics, this only affect the current shell, it does not affect shells you spawn.
The more informative form can be
exit() { local ans local line read -p "You really want to exit this? " line ans=$(echo $line) case "$ans" in Y);; y);; *)kill -INT $$;; esac unset -f exit exit $1}
You'll need to add return
statements to each of your functions to check the return value of any functions they call in turn. Sourcing a file is like cutting and pasting the code into the current context, with the minor exception of variables like $BASH_SOURCE
.
Alternatively you could define fn
as a shell script, so that exit
will do what you want (unless a fork is too expensive).