159

When I'm done with Terminal, I want to exit it. Right now, I have three options:

  1. killall Terminal. It will end the process, but rather abruptly. I don't think this is the best idea.

  2. Call exit. I've changed the settings so exit closes Terminal. The app still appears open in the Dock though, and doesn't do what I want it to do.

  3. Right click>Quit. However, this isn't a Terminal command to exit it.

So now, what is the way I should exit and close Terminal? I've heard something about osascript but I'm not too sure. I want to exit and close, so that Terminal is no longer open, both as a window and as a process.

libra
  • 673
  • 1
  • 10
  • 30
darksteel
  • 1,599
  • 2
  • 9
  • 4
  • Like `kill`, `killall` defaults to sending `SIGTERM`, which can be caught. Some programs catch it, clean up after themselves and exit gracefully. No idea if `Terminal.app` is among them. However, I suspect this would be better on [Ask Different](http://apple.stackexchange.com/) or Super User in a pinch as it does not seem to have any programming content. – dmckee --- ex-moderator kitten Apr 06 '11 at 03:36
  • 3
    I think the question would be better phrased “How to Quit Terminal after the last shell exits?”. – Chris Page Jan 05 '12 at 08:08
  • Possible duplicate of [Close Terminal window from within shell script (Unix)?](http://stackoverflow.com/questions/8798641/close-terminal-window-from-within-shell-script-unix) – bob Mar 16 '17 at 17:19

15 Answers15

272

in Terminal.app

Preferences > Profiles > (Select a Profile) > Shell.

on 'When the shell exits' chosen 'Close the window'

Sandy Chapman
  • 11,133
  • 3
  • 58
  • 67
Duke
  • 3,226
  • 1
  • 18
  • 23
  • @PrabhuKonchada wouldnt be too terrible if you wanted to use that space to feed user output but otherwise insanikov's answer would have done quife fine for me – Frankenmint Dec 26 '15 at 03:08
  • Obvious and clean, perfect. – aydinugur Aug 28 '17 at 23:59
  • 18
    In the newer versions of OS X, this option can be found under 'Preferences > Profiles > (Select a Profile) > Shell. – XtraSimplicity Dec 23 '17 at 10:15
  • You can use the link for more and step wise step info https://www.cnet.com/news/terminal-tip-close-windows-after-clean-exit/ – Krishan Kumar Mourya Jan 12 '18 at 07:37
  • 3
    You can also select all the profiles, and make the change to all at once. –  Jun 08 '18 at 14:34
  • 2
    In macOS Sierra 10.13.5, there is also the option "Close if the shell exited cleanly." This option may also be present in earlier versions of the OS. – DrZoo Jun 29 '18 at 04:56
  • On Mac OS Monterey this doesn't work. It only closes the window, but you still need to press Cmd-Q to quit the app, or in other words, as the original question noted: "The app still appears open in the Dock though, and doesn't do what I want it to do." – mastazi Nov 25 '22 at 06:17
42

Actually, you should set a config on your Terminal, when your Terminal is up press +, then you will see below screen:

enter image description here

Then press shell tab and you will see below screen:

enter image description here

Now select Close if the shell exited cleanly for When the shell exits.

By the above config each time with exit command the Terminal will close but won't quit.

AmerllicA
  • 29,059
  • 15
  • 130
  • 154
  • This doesn't solve much in my case. Indeed, the initial window does close, but then a fresh terminal window opens. – Lethargos Nov 21 '21 at 16:47
  • @Lethargos, what? I didn't understand your issue!, please explain a little bit more. – AmerllicA Nov 21 '21 at 18:14
  • I run an app (firefox) as a shell script, because I want to choose add a cli parameter (-P profile_name). In order for it to work in finder, I add the '.command' extention. I set the 'close if the shell exited cleanly' option. The consequence of that is that the initial window exists, indeed, but the terminal (actually) remains open. That's it. (mbp m1, monterey 12.0.1) – Lethargos Nov 21 '21 at 19:13
  • @Lethargos, it's a very important case, I think it is better to post a question about this issue, the answer is using `&` for closing in the `.command` file. I can help you. if you are agreed, post a question with enough explain and screenshot, I will send the answer in full description there with a good example. – AmerllicA Nov 21 '21 at 19:36
  • I am using `&` and `nohup` at that. But yes, I was planning to post a question about this, as I still haven't found anything else on the internet. So I'll do it soon :) – Lethargos Nov 21 '21 at 20:00
  • I've posted my question :) – Lethargos Nov 21 '21 at 21:45
38

How about the good old Command-Q?

Chris Page
  • 18,263
  • 4
  • 39
  • 47
holygeek
  • 15,653
  • 1
  • 40
  • 50
  • He does mean command Q. Is this a good method though? It seems no different than killall to me. – darksteel Apr 06 '11 at 00:57
  • @darksteel: Command-Q is the proper way to close an app on the Mac. Doing a killall is probably OK as well since the app is told to terminate cleanly. – tomlogic Apr 06 '11 at 00:59
  • 3
    Yeah I meant to type "Command" but it came out as Ctrl. I'd say it's a case of PEBBAF (Problem exist between brain and fingertips). – holygeek Apr 07 '11 at 02:05
  • kill/killall is not a good way to terminate an application, generally. That could discard unsaved state, or save state that doesn’t correctly reflect how the application would look had you Quit it. Mac OS X does have a “Sudden Termination” feature where programs can indicate when they’re in a state where it’s safe to kill them (with SIGKILL, in fact), and if you checked the state of Terminal with Activity Monitor to verify that it’s safe to kill it first (show the “Sudden Termination” column and look for “Yes”), you could then do so, but you can’t easily automate that workflow. – Chris Page Jan 05 '12 at 08:18
  • We can always use command-Q after exit, which closes all process and then closes the terminal. – Optimus Prime May 19 '17 at 11:57
33

In the Terminal app, Preference >> Profiles tab.

Select the Shell tab on the right. enter image description here

You can choose Never Ask before closing to suppress the warning.

DocZerø
  • 8,037
  • 11
  • 38
  • 66
Blaise
  • 21,314
  • 28
  • 108
  • 169
18

You could use AppleScript through the osascript command:

osascript -e 'tell application "Terminal" to quit'
icktoofay
  • 126,289
  • 21
  • 250
  • 231
  • I hadn't tried the osascript command, but there's two issues I have with this: 1. It's rather long for a command to quit. Shouldn't it be simpler? I like in Windows how exit terminates command prompt. 2. It still prompts me to quit Terminal. I want it to just go through the motions. A window came up asking if I really wanted to quit. – darksteel Apr 06 '11 at 00:55
  • 1
    @darksteel: 1. Add an alias in your `.bashrc` (or equivalent if you use a different shell). I'm still thinking of an answer to 2. – icktoofay Apr 06 '11 at 01:01
  • I'm thinking command Q works best. But I'm curious as to how to setup an alias properly. There's a few commands like "ping google.com" that I do often enough that I might prefer an alias. – darksteel Apr 06 '11 at 01:06
  • @darksteel: This is way overthinking it, but to prevent 2, you could start a daemonized process that sleeps about a second, and then `exit` after starting the daemonized process. After the daemonized process has waited a second, it could call `osascript` as described. – icktoofay Apr 06 '11 at 01:07
  • @darksteel: You can also trigger a keypress through `osascript`: `tell application "System Events" to keystroke "a" using command down`. This will probably have the issue though, too. – icktoofay Apr 06 '11 at 01:09
  • @darksteel: I just tried the daemonize method, and apparently `osascript` doesn't like being run from a daemon. Honestly I think `killall` is just fine. – icktoofay Apr 06 '11 at 01:12
  • 1
    Doesn't killall try to close the app properly anyways? I'm pretty sure it tells the app to close and lets it try to close, and only abruptly ends the process if the process can't terminate itself. I would assume that if there's no problems, it will terminate properly and then it wouldn't matter. – darksteel Apr 06 '11 at 01:16
12

In a terminal window, you can type:

kill -9 $(ps -p $PPID -o ppid=)

This will kill the Terminal application process, which is the parent of the parent of the current process, as seen by the kill command.

To close a Terminal window from within a running script, you need to go up one more level in the process hierarchy like this:

kill -9 $(ps -p $(ps -p $PPID -o ppid=) -o ppid=) 
GOTO 0
  • 42,323
  • 22
  • 125
  • 158
9

I 've been using ctrl + d. It throws you out into the destination where You've started the sqlite3 command in the first place.

kalafun
  • 3,512
  • 6
  • 35
  • 49
9

osascript -e "tell application \"System Events\" to keystroke \"w\" using command down"

This simulates a CMD + w keypress.

If you want Terminal to quit completely you can use: osascript -e "tell application \"System Events\" to keystroke \"q\" using command down"

This doesn't give any errors and makes the Terminal stop cleanly.

insanikov
  • 223
  • 2
  • 7
5

You can also use this convoluted command, which does not trigger a warning about terminating its own process:
osascript -e "do shell script \"osascript -e \\\"tell application \\\\\\\"Terminal\\\\\\\" to quit\\\" &> /dev/null &\""; exit

This command is quite long, so you could define an alias (such as quit) in your bash profile:
alias quit='osascript -e "do shell script \"osascript -e \\\"tell application \\\\\\\"Terminal\\\\\\\" to quit\\\" &> /dev/null &\""; exit'

This would allow you to simply type quit into terminal without having to fiddle with any other settings.

David P.
  • 335
  • 3
  • 12
3

Use the osascript command in your code as icktoofay mentioned: osascript -e 'tell application "Terminal" to quit'

Then, open Terminal preferences, go to Settings > Shell, and set "Prompt before closing:" to "Never." Terminal should now quit completely (not remain open in your dock) and ignore the prompt before quitting. If you have only one Terminal window open and the osascript command is your last line of code, it should wait for whatever command you ran before to finish.

This would not be ideal if you are running scripts in the same window or other windows in the background (for instance, you may run a command in the background and continue using the current window for other commands if the first command is followed by an ampersand); be careful!

If you wrap the osascript code in a shell script file, you can probably call it with whatever pithy file-name you give it---as long as it is in Terminal's search path (run echo $PATH to see where Terminal looks for scripts).

John
  • 649
  • 5
  • 6
2

This is what I did for a command I just wrote. I wanted to be able to create a "shortcut" to the Backup directory for Apple Configurator that worked on different computers, but since it's relative to the user's home directory, I needed to make it a .command file. Here are the contents:

#!/usr/bin/env bash

open "${HOME}/Library/Application Support/MobileSync/Backup"
(sleep 0.1 ; osascript -e 'tell application "Terminal" to quit') &
Timothy Zorn
  • 3,022
  • 2
  • 20
  • 18
1

I've been using

quit -n terminal

at the end of my scripts. You have to have the terminal set to never prompt in preferences

So Terminal > Preferences > Settings > Shell When the shell exits Close the window Prompt before closing Never

Jon
  • 35
  • 1
1

Create a script:

cat ~/exit.scpt

like this:

  • Note: If there is only one window, just quit the application, else simulate command + w to close the tab)

tell application "Terminal" set WindowNum to get window count if WindowNum = 1 then quit else tell application "System Events" to keystroke "w" using command down end if end tell

Then add a alias in your *shrc

just like vi ~/.bashrc or zshrc (anything else?)

add it: alias exit="osascript ~/exit.scpt"

And source the ~/.bashrc or reopen your terminal.app

Sin
  • 316
  • 2
  • 4
0

I tried several variations of the answers here. No matter what I try, I can always find a use case where the user is prompted to close Terminal.

Since my script is a simple (drutil -drive 2 tray open -- to open a specific DVD drive), the user does not need to see the Terminal window while the script runs.

My solution was to turn the script into an app, which runs the script without displaying a Terminal window. The added benefit is that any terminal windows that are already open stay open, and if none are open, then Terminal doesn't stay resident after the script ends. It doesn't seem to launch Terminal at all to run the bash script.

I followed these instructions to turn my script into an app: https://superuser.com/a/1354541/162011

James L.
  • 9,384
  • 5
  • 38
  • 77
-3

If this is a Mac you type 'exit' then press return.

Tremaine
  • 45
  • 11