Terminal Tip: Close windows after clean exit

A quick setting change may help reduce window clutter for OS X Terminal users.

If you regularly script tasks in the Terminal, you might find at times that handling windows can be a burden. While an unused Terminal window can quickly be closed by pressing Command-W or clicking the red button at the top of the window, if you run numerous scripts then you may be left with a number of windows to close.

One way you can better manage these windows is to change the default Terminal behavior for when a terminal shell exits. By default, the Terminal will keep the window open and display the text "[process completed]," leaving you with a nonfunctional window that you have to close manually. However, if you change this behavior you can run commands and scripts in ways that will close the window when done.

To do this, go to the Terminal's preferences, then to the Settings section, and select the "Shell" tab. Then, change the shell exiting behavior to "Close the window" or preferably to "Close if the shell exited cleanly."

Terminal window closing and warning settings.
Set the Terminal to close if a shell exits cleanly. You can also customize warnings and prompts when closing windows, either always, never, or only for specific processes. Screenshot by Topher Kessler/CNET

With this setting in place, if the shell exits, then the window will close as well. This way, you can run commands or scripts and when the script or command is finished then the terminal window will close. For example, if you want to run the command "top" to get a view of the system resources being used, then you can do so by running the following command:

top; exit

With this command, the "top" program will run and show you the system's process list, but when you quit "top," the shell will then exit and the window will immediately close. You can similarly apply "; exit" to the end of any script you run to close the shell and Terminal window when finished.

Terminal window process completed message
When a process is completed, the Terminal window will remain open and show this message, but with these preference settings the window will close. Screenshot by Topher Kessler/CNET

You can also manually close windows by simply typing "exit," instead of pressing Command-W, which may be slightly easier or simply just an alternative to use in some contexts.

Questions? Comments? Have a fix? Post them below or e-mail us!
Be sure to check us out on Twitter and the CNET Mac forums.

Featured Video