Exit the bash function, not the terminal

TL;DR

Use return instead of exit AND run your script with source your-script.sh aka. . your-script.sh

Full details

If launching a script with an exit statement in it, you have to launch it as a child of you current child.

If you launch it inside the current shell of started with your terminal session (using . ./<scriptname> any exit will close the main shell, the one started along your terminal session.

If you had launched your script like bash ./<scriptname> (or any other shell instead of bash), then exit would have stopped your child shell and not the one used by your terminal.

If your script has executable permissions, executing it directly without giving the name of the shell will execute it in a child shell too.

Using return instead of exit will allow you to still launch your script using . ./<script name> without closing the current shell. But you need to use return to exit from a function only or a sourced script (script ran using the . ./<scriptname> syntax).


The only way that the given script is able to terminate the shell session (and therefore the terminal) is by you sourcing the script (to install the installZook function in the current shell session), and then running the function in the shell.

If what you are showing is only a portion of a larger script containing a call to the installZook function, then the function can still only cause the terminal to terminate if the script is sourced, but not through being run as an ordinary shell script.

exit terminates the current shell session. When the function executes exit it terminates the shell that called it.

return returns from a function (or a sourced script file). If the function, instead of exit, used return, it would return control to the calling environment (probably the interactive shell that you called the function from) without exiting it.

If you manually run the installZook function from the shell, then all you should need to do is to change the exit to return. If there's another piece of code in your script that calls the function (and which you are not showing), then that piece of code needs to additionally react to the return status of the function.

For example

installZook || return

If this was part of no function in the script, and if you sourced the script, it would return control to the shell if the function returned a non-zero exit code.