Search code examples
bashubuntu

Exit running script without exiting shell


I have a script library that uses a lot of exit commands if some condition occurs. Each time an exit is hit, the shell is closed.

I saw many answers regarding this question that suggest changing the script, but my scripts are 3rd party and I assume the authors didn't mean that the shell should close so I assume there is some other way to run.

How do I run these script so that only the script stops but the shell remains open. Currently I use . script.sh.


Solution

  • Instead of launching your script as . script.sh, you can launch it bash script.sh. When you launch it with bash then a child process for bash will be opened and your script will execute in the child shell and exit statements will make child shell closed and have no scope for parent shell or main shell.