To prevent source test.sh from exiting the current shell when test.sh contains exit, you need to ensure that the exit command in the script does not terminate the parent shell.
When you source (or .) a script, it runs in the current shell context. So, if the script contains exit, it will exit the current shell.
exit with returnIf test.sh is meant to be sourced (not executed as a standalone script), use return instead of exit:
bashreturn
return exits the current subshell or sourced script, not the parent shell.If you want to keep exit in the script, run it in a subshell:
bashbash test.sh
exit only affects that subshell.exitIf you must keep exit and want to source the script, add a guard:
bashif [[ "${BASH_SOURCE[0]}" == "$0" ]]; then exit fi
exit is skipped.For scripts meant to be sourced, use return. For standalone scripts, use exit.
Vibe can make mistakes. Check answers. Learn more