Why doesn't "exit" close a Terminal.app window on Mac OS X?
$ exit
logout
[Process completed]Is there a way to close the window without using the mouse?
07 Answers
A window displayed by Terminal.app is just the frontend for the process you choose to run inside of it - in your case, a Unix shell. When you exit the shell, the Terminal.app does not close the window by default, so you have the possibility to inspect the output from whatever command you ran, after it finishes.
You can change your preferences here
Terminal Preferences -> Settings -> Shell:
to either
- always close the window, whatever the exit status of the shell was
- close the window if the shell exited cleanly
- keep the window open (the default)
Besides that, you can (almost) always close windows in OSX with Cmd-W, so you don't need mouse even if it doesn't close automatically.
One more hint: I like hitting Ctrl-D instead of typing exit. Two keys vs. five.
10Command + Q -> closes the application/process.
Command + W -> closes window/instance
1Yes there is. For example you can use AppleScript to achieve it:
osascript -e 'tell application "Terminal" to close first window'The first window is always the currently active window. That's the one you want to close.
Before closing the window, the Terminal may ask you, if you really want to close the window.
This depends on your settings. You may have chosen to 'close the window only if the shell exited cleanly or no other processes are running apart from …'. (This may be the default setting.)
In that case adding & exit to the command closes the window immediately and without asking.
osascript -e 'tell application "Terminal" to close first window' & exit 2 Actually, for this requirement, you should set some config to your Terminal. follow below instructions and you will close your Terminal just with an exit command.
When the Terminal is up, press ⌘+, to open the prefrences window. then you will see below screen:
Then press shell tab and you will see below screen:
Now select Close if the shell exited cleanly for When the shell exits.
Your Terminal is ready for the exit just with an exit command.
I also suggest against the killall suggestion. As suggested modify the settings in your preferences to close window if shell exit was successful. If you're REALLY LAZY (like me), open up your bash profile and add an alias. I have mine set so all I have to do is type 'q'.
0If you want to terminate the application itself from the commandline:
killall Terminal 2 I needed a variant of this solution for an application that required the user to enter their password in terminal for a sudo script. Catalina's security makes sending keystrokes a real pain, so hopefully this solution helps someone else in the future:
tell application "Terminal" activate do script "sudo say --rate=140 Intergalactic && kill -9 $$" delay 5 repeat try do shell script "ps a | grep -v grep | grep 'sudo say --rate=140 Intergalactic'" delay 0.5 on error exit repeat end try end repeat close front window
end tellHere is an explanation for some of the weird looking stuff:
kill -9 $$: This is basicallyexitthat works; for some reasonexitwasn't working for me for my application. You want to kill the shell process so that the user doesn't get prompted to terminal a running terminal... This will allow it to silently close.try...do shell script: This checks to see if the command is still running, and if so, the loop keeps going so that window doesn't get killed. Ifgrepfinds a match, it returns an error code 0 (AppleScript is happy); ifgrepdoesn't find a match, it returns an error code 1 and then your script stops running when the party is just about the begin.close front window: Yes, I realize #closefrontwindowisconsideredharmful. There is a 500ms gap where either another terminal window could come into focus, or the user could Command+Tab to another Terminal window or something... I figure if you are interacting with the window, it's probably an edge case. Feel free to come up with a better way and let us know!
I'll probably be occasionally maintaining this thing here: