Here’s how to close the macOS terminal automatically when a process ends

If you’re a fan of shell scripting in macOS, you’ve probably noticed that once you run one, you get a useless terminal window when you run one. You can fix this in the terminal settings.

This method only works if you are running a shell script outside of the. start bashfor example, by clicking it in the Finder or setting a custom hotkey to open the program. Otherwise, you will just be directed back to the Command Prompt. You can always use killall Terminal to create the terminal app close itself out of a script, but it will close any open terminal window, so it’s not ideal.

Change this behavior in the profile settings

Open the Terminal app from the Dock or your applications folder, then open Settings by pressing Command + Comma.

In the Settings window, switch to the Profiles tab. The default profile (the one at the top) should be selected by default. In Settings, click the Shell tab on the right, then click the When Shell Exits drop-down menu.

The drop-down menu is set to “Not” by default close the window, ”but you should change this to“ Close when the shell exits cleanly ”.

Close MacOS Terminal when ending the process

You can do it too close every time, but that way you will still get an error message whenever a process is using a Exit-Status other than zero is terminated. Keep in mind that you may need to explicitly exit the script using this exit Command to get this behavior in all cases.

Command to exit the shell script

However, if you start from the Finder, the Exit-Command automatically appended to the script.

Alternatively, you can use iTerm

iTerm2 settings

iTerm2, a popular terminal replacement for macOS, will automatically close the window when a shell script exits. If you’re already using iTerm as your default terminal, you may have noticed that shell scripts launched from the Finder will still open with the default Terminal app. This means that unless you choose to open scripts with iTerm, you will still have the same problem.

You can change which application scripts open in by right-clicking the script in the Finder and then selecting Get Info.

macOS script open with iTerm

A drop-down menu will appear to change what opens this script. Set it to iTerm and click “Change All” to apply this change to each script.

If you already have an iTerm window open, by default it will launch in a separate tab rather than a separate window and the tab will close automatically when it’s done.

Related Posts