8

I want to close the Terminal using the terminal command-line.

But when I enter the exit command, Terminal only logs out the session but does not close the app window. Is there any way to also close the app.

I'm running macOS Catalina 10.15.6.

enter image description here

2
  • 2
    The title ask how to close the APP while the body ask how to close only the window. It should be clarified (the solution in the accepted answer doesn't close the app, while a solution given in another answer do close the app).
    – JFL
    Commented Aug 16, 2020 at 15:38
  • thank for the reply , now I had changed it. Commented Aug 17, 2020 at 16:10

1 Answer 1

21

In Terminal > Preferences… > Profiles > Shell, the default setting for When the shell exits: is: Don't close the window

This can be changed to: Close the window

Then the exit command will close the window.

enter image description here


Edit to address the edited Question:

If you want to close the window using the exit command you can do as suggested in the first part of this answer, however, if you also want to automatically and gracefully close Terminal as well, as part of your script or in place of the exit command as shown in your question, then you can do the following:

First create a simple AppleScript application using the following example AppleScript code in Script Editor:

tell application "Terminal" to if (busy of windows) does not contain true then quit

Saving it as an application named e.g. QuitTerminal in the main Applications folder.

After saving it, open Terminal and then open e.g. QuitTerminal in order to trigger allowing permissions under System Preferences > Security & Privacy > Privacy

Once this is done you can then use the following at the end of your script or in place of the exit command:

open -a "OuitTerminal"

Example based on command shown in your question:

/Users/harikrishna/Desktop/youtube.sh ; open -a "OuitTerminal"

This will only close Terminal if there are no running processes in any of the windows in Terminal.

Note: If you do not want to see e.g. OuitTerminal show in the Dock, you can modify the application using the following command in Terminal:

defaults write '/Applications/OuitTerminal.app/Contents/Info.plist' LSUIElement -bool yes

If you do not care about any running process you can force close Terminal from the script or command using killall Terminal in place of exit; however this is messy and I do not recommend using it over what's presented above.

3
  • Thanks for the answer , it also worked.But I faced a difficulty while writing apple script and later I did it .But I don't understand as the previous answer also close the window . Could you pls tell what happens or what is the difference between the exit and the QuitTerminal script you have shared here. Commented Aug 18, 2020 at 1:51
  • @Hari krishna Goli, If all you are trying to do is close the window and not also Terminal then just use my original answer, which is what I assumed you were trying to do. If you also want to close the Terminal app and not just one of its windows then you'll need to use what I added after you edited your original question. In the first part of my answer making the change in the preferences allows the exit command to close the window it's executed in, but not the application. My updated answer provides a way to gracefully close the application, not just a window. Commented Aug 18, 2020 at 3:05
  • @Hari krishna Goli, The reason for the AppleScript application is to only close Terminal if no processes are running in any of its windows. Commented Aug 18, 2020 at 3:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .