Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've recently acquired a new Mac with OS X Lion, and I'm tuning it for my needs.

I was trying to configure autoclose on Terminal using its preferences (as I've done with Tiger and Snow Leopard) but it's not working. I've set "Close if the shell exited cleanly" but when I type exit, the window gets closed, but the is still open. Its menu is visible, and with alt+tab I can see it running.

Any suggestions?

share|improve this question
up vote 7 down vote accepted

This option has never closed the Terminal application.

Note that the other options are titled Close the window and Don't close the window (emphasis mine), making it clear that this setting refers to only closing the window, not the entire application.

You could define the following for your shell, e.g. in ~/.bash_profile for bash:

function exit {
    osascript -e 'tell application "Terminal" to quit' 

Then add osascript to the list of applications that don't require confirmation before quitting (in Preferences > Settings > Shell). Type exit to exit Terminal, unless there are other tabs with blocking programs running.

You can give it a different name to separate from a regular exit, of course. I like quit for this.

share|improve this answer
Using an AppleScript to quit Terminal is a good idea, but you should probably make it only quit if there are no other windows open: osascript -e 'tell application "Terminal" to if not ((count windows) > 1) then quit' – Chris Page Sep 9 '11 at 3:51
@chris Possible, a matter of taste though. I let terminal's exit protection when running processes handle that. – Daniel Beck Sep 9 '11 at 4:59
Daniel Beck, my point is that if you set it up so that any time you exit a shell it runs this, you won't be able to close any terminals unless you want to close them all and quit Terminal. The question asked by Mtorres is how to get Terminal to quit when the last terminal window is closed; to do that, only quit Terminal when there is only one window open. – Chris Page Sep 9 '11 at 5:42
@Chris Possibly. We don't know whether the user actually uses multiple Terminal windows. In that regard, your script is technically incomplete, since it ignores possibly existing other tabs; and does nothing (not even close the tab/window) when there are multiple windows. The question makes no mention of tabs either. Without further feedback, we can't know for sure. Also, there's a reason I suggest giving a different name to the function, that way, he actually has a choice. – Daniel Beck Sep 9 '11 at 6:25
Hello both of you! I usually work with multiple tabs, but I don't like to close them all at once. I like your little discussion about it, and I'm staying with Chris suggestion. Anyhow, Daniel pointed right to the solution. Thanks! – mTorres Sep 11 '11 at 16:51

As a neat solution, try-

open -a /Applications/Utilities/ *.py OR open -b *.py

For the shell launched, you can go to Preferences > Shell > set it to exit if no error.

This I often use when I need to almost simultaneously run many scripts/commands.

That's it.

share|improve this answer

To have Terminal quit after you close the last terminal/shell, you can have your shell run an AppleScript when exiting:

# Quit Terminal when this shell exists if there are no other terminals open.                                                                                                                    
if [ "$TERM_PROGRAM" == "Apple_Terminal" ]; then
    quit_terminal_when_no_terminals_remain() {
        osascript -e 'tell application "Terminal" to if running and (count every tab of every window whose tty is not "'"$(tty)"'") is 0 then quit'
    trap quit_terminal_when_no_terminals_remain EXIT

The test for Apple_Terminal ensures this code only takes effect when running inside Terminal.

Since this runs asynchronously, the script may run before or after the terminal containing it is closed (though it usually runs after), and if this terminal is closed because the user quit Terminal, Terminal may no longer be running when the script runs, therefore:

  1. First it checks whether Terminal is still running. If not, it does nothing.
  2. It only quits if there are either no terminals open or only the one for the current tty. It checks whether there are any tabs other than the one for the tty device to which the current shell is connected "$(tty)".

Note that if you invoke any other shell code that traps EXIT, these will interfere with one another. The solution is to create another function that calls the others and "trap the_other_function EXIT" to invoke everything when the shell exits.

By the way, as always, if this functionality is important to you please file an enhancement request with Apple. Ask for a preference setting to make Terminal quit when there are no more windows open:

share|improve this answer

A much much easier an elegant solution would be to set an alias in your .bash_profile as this command:

alias quit="killall Terminal"

Beautiful, uh?

share|improve this answer
This will abort, no matter what is running, all of your Terminal sessions. The programs will probably receive sighup and terminate in the middle of what they're doing. This only works if you're religiously using screen or tmux, otherwise the first time you enter it by accident you'll regret it. – Daniel Beck Nov 24 '11 at 19:42

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.