0

I need some help: (On macos, bash shell)

If I run a .sh file which calls e.g. exit 1 (any exit code) my terminal session ends (and the iterm2 tab/window closes). I'm calling the script like this $ . myscript.sh

I'm pretty sure it should not be like that or was not like this a while before.

jacksbox
  • 911
  • 1
  • 11
  • 24
  • 1
    Do you control the contents of the script? If no: see [bash source a file that has an exit command at the end](https://stackoverflow.com/q/52063180/3266847); if yes: see [Terminal closes when I source my script (run with dot at the start)](https://stackoverflow.com/q/53596107/3266847) – Benjamin W. May 25 '21 at 13:58
  • In iTerm2 preferences: General -> Closing -> uncheck "Quit when all windows are closed" – glenn jackman May 25 '21 at 14:02

2 Answers2

2

Using:

. myscript.sh

You are actually running the script in the existing shell or "sourcing" the script. With exit at the end of the script, this means that the terminal session will also exit

Alternatively:

./myscript.sh

or

bash myscript.sh

Will run the script in a separate bash shell and stop the terminal session from exiting.

Raman Sailopal
  • 12,320
  • 2
  • 11
  • 18
1

Instead of . myscript.sh you can run ./myscript.sh which will run it in a separate bash shell and will not exit the current session.

If you control the content of this .sh file, and you do want to source the script - simply return 1 instead of exit 1, and use proper error handling.

sheldonzy
  • 5,505
  • 9
  • 48
  • 86