How to close macOS terminal automatically when a process exits

Contents

MacOS Terminal Stock Lede

If you are a fan of shell scripts on macOS, You've probably noticed how running one will leave you with a useless Terminal window after it completes. You can fix this from Terminal settings.

This method only works if you are starting a shell script outside of bash, such as clicking on it in the Finder or setting a custom hotkey to open the program. Opposite case, will go back to command prompt. You can always use killall Terminal to make the Terminal app close itself from within a script, but that will close all open Terminal windows, so it is not ideal.

Change this behavior in profile settings

Open the Terminal application from the Dock or your Applications folder, then open the settings by pressing Command + Coma.

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

The drop-down menu defaults “Do not close the window”, but you'll want to change it to “Close if the shell came out cleanly”.

MacOS terminal closing when exiting the process

You can also close it every time, but this way you will still get an error message if a procedure exits with a non-zero exit status. Note that you may need to exit the script explicitly with the exit command to get this behavior in all cases.

Shell script output command

Even if you are starting from Finder, the output command is automatically added to the script.

Alternatively, use iTerm

ITerm2 configuration

iTerm2, a popular Terminal replacement for macOS, will automatically close the window when a shell script exits. If you already use iTerm as your default terminal, You may have noticed that shell scripts started from Finder are still open with the standard Terminal app. This means that you will still have the same problem unless you configure the scripts to open with iTerm.

You can change which applications the scripts open in by right-clicking the script in Finder and then selecting “Get information”.

macOS open script with iTerm

There will be a drop down menu to change what this script opens with. Set it to iTerm and press “Change everything” to apply this change to each script.

By default, if you already have an iTerm window open, it will launch in a separate tab instead of a separate window, and the tab will automatically close when ready.

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.