How do I close the Terminal in OSX from the command line?

72

17

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?

user242065

Posted 2010-06-29T02:11:47.523

Reputation:

Answers

92

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

  1. always close the window, whatever the exit status of the shell was
  2. close the window if the shell exited cleanly
  3. 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.

Amadan

Posted 2010-06-29T02:11:47.523

Reputation: 1 465

12In Yosemite it's: preferences -> profiles -> shell -> "when the shell exits"... Shell is whichever shell you have selected in General-> On startup open... – cloudsurfin – 2015-09-22T17:21:56.873

Once the shell is exited, is it possible to start a new shell in the same window? If so, how? – Sodved – 2016-07-09T02:14:53.747

@Sodved: I don't think it is, but could be wrong. – Amadan – 2016-07-11T05:34:16.957

This does not answer the question that was asked, as it does not propose a command line operation. – Chris Stratton – 2017-01-04T17:59:36.050

@ChrisStratton You're correct that it does not directly answer the question asked, but it does facilitate the use of the exit command which the OP was using originally. – jdersen – 2018-12-22T03:57:10.107

Ctrl-D in not working for me. – roottraveller – 2019-07-03T04:52:39.790

@roottraveller: Reading "is not working for me" is not working for me. You need to explain what happens instead, for there to be any chance that I'd recognise what the issue is. – Amadan – 2019-07-03T04:55:12.157

@Amadan yes Ctrl-D is just making a split horizontally in the terminal. however, I am able to close using Command + W as mentioned in another answer below. – roottraveller – 2019-07-03T05:44:32.573

@roottraveller: Command-D is "split horisontally". Ctrl-D is EOT (End of Transmission) control character, which bash understands as "I've had enough". – Amadan – 2019-07-03T05:54:31.277

@Amadan ohh my bad. it's working. I'm not even high. idk what I was thinking. – roottraveller – 2019-07-03T06:24:23.193

23

Command + Q -> closes the application/process.

Command + W -> closes window/instance

prem

Posted 2010-06-29T02:11:47.523

Reputation:

10

Yes 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

Thomas.S

Posted 2010-06-29T02:11:47.523

Reputation: 101

This is almost right, but in actuality the "first window" is not always the one running the command. – Chris Stratton – 2017-01-04T18:41:43.800

This technique works well after open is called to add a new Terminal profile. – Josh Habdas – 2019-10-16T09:25:58.657

4

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:

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.

Your Terminal is ready for the exit just with an exit command.

AmerllicA

Posted 2010-06-29T02:11:47.523

Reputation: 141

1Awesome! Was looking for this for so long. Thanks – Vinayak – 2019-10-31T10:51:52.003

1

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

bran.io

Posted 2010-06-29T02:11:47.523

Reputation: 71

-1

If you want to terminate the application itself from the commandline:

killall Terminal

Just Jake

Posted 2010-06-29T02:11:47.523

Reputation: 688

very good solution. We just need to exit terminal so there is no asking!! best answer. – Mehdico – 2019-09-19T19:33:57.713

4that is considered harmful. Apart from the fact tha killall does different things on different Unix versions, it's not nice to kill an application instead of asking it to just quit. – Florenz Kley – 2012-11-15T13:55:01.607