164

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.

3
  • 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 or Super User in a pinch as it does not seem to have any programming content. Apr 6, 2011 at 3:36
  • 3
    I think the question would be better phrased “How to Quit Terminal after the last shell exits?”.
    – Chris Page
    Jan 5, 2012 at 8:08
  • Possible duplicate of Close Terminal window from within shell script (Unix)?
    – bob
    Mar 16, 2017 at 17:19

15 Answers 15

276

in Terminal.app

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

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

7
  • @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 Dec 26, 2015 at 3:08
  • Obvious and clean, perfect.
    – aydinugur
    Aug 28, 2017 at 23:59
  • 21
    In the newer versions of OS X, this option can be found under 'Preferences > Profiles > (Select a Profile) > Shell. Dec 23, 2017 at 10:15
  • 3
    You can also select all the profiles, and make the change to all at once.
    – user2191247
    Jun 8, 2018 at 14:34
  • 3
    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, 2018 at 4:56
47

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.

6
  • This doesn't solve much in my case. Indeed, the initial window does close, but then a fresh terminal window opens.
    – Lethargos
    Nov 21, 2021 at 16:47
  • @Lethargos, what? I didn't understand your issue!, please explain a little bit more.
    – AmerllicA
    Nov 21, 2021 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, 2021 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, 2021 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, 2021 at 20:00
38

How about the good old Command-Q?

5
  • He does mean command Q. Is this a good method though? It seems no different than killall to me.
    – darksteel
    Apr 6, 2011 at 0: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 6, 2011 at 0: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 7, 2011 at 2: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 5, 2012 at 8:18
  • We can always use command-Q after exit, which closes all process and then closes the terminal. May 19, 2017 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.

2
  • 6
    I achieved this with When shell exits: "close the window"
    – Anup
    May 2, 2017 at 11:49
  • 1
    This answer didn't work for me by choosing "Never". However @Anup's comment about "Close the window" did the trick for me.
    – Ultimater
    May 17, 2019 at 4:39
18

You could use AppleScript through the osascript command:

osascript -e 'tell application "Terminal" to quit'
7
  • 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 6, 2011 at 0: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 6, 2011 at 1: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 6, 2011 at 1: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 6, 2011 at 1:07
  • 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 6, 2011 at 1: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=) 
2
  • 11
    FYI, this solution kills other terminal windows too.
    – tresf
    Sep 18, 2016 at 0:45
  • 1
    And don't use kill -9 unless you know other signals won't work.
    – tripleee
    Feb 6, 2019 at 5:36
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.

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.

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.

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).

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') &
1
  • This worked the cleanest for me with no warnings. Thanks!
    – Joe Steele
    Aug 19, 2022 at 17:07
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

0
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

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

-3

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

0

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