MacOS – How to close the Terminal app using the terminal command-line(“exit”) in macOS Catalina

catalinamacosterminal

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

Best Answer

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.