Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close Terminal window from within shell script (Unix)?

People also ask

How do I close shell terminal?

In the Terminal app on your Mac, in the window running the shell process you want to quit, type exit , then press Return.

How do I close a Windows shell script?

Using exit 0 will cleanly terminate the script. Whether Terminal window stays open is user-configurable.


Using exit 0 will cleanly terminate the script.

Whether Terminal window stays open is user-configurable. The default is to always stay open. To change this:

Terminal.app > Preferences > Profiles > Shell
    - "When the shell exists:"
        > Close if the shell exited cleanly
    - "Ask before closing:"
        (•) Never
        -- OR --
        (•) Only if there are....

When "Close if shell exited cleanly" is used, the script will close the window if the exit result is 0, which is the default if nothing went wrong.


Since you don't want to delete all Terminal windows, first change the name of your window from "Terminal" to something else:

echo -n -e "\033]0;My Window Name\007"

Then at the end of the script, use:

osascript -e 'tell application "Terminal" to close (every window whose name contains "My Window Name")' &


You can use apple script to quit the terminal app. Add the following to your script -

osascript -e 'tell application "Terminal" to quit'

This will give you a popup confirming to close the app. You can disable this in Terminal preferences.

Alternatively, you can also use killall command to quit the app. The following would work just as well.

killall Terminal

Note:

Just as a side note, you can freely add the above commands to your script and it would work as you want. However, there are few caveats. First being you will limit the ability of your script to work on different boxes. Secondly, it would be safer to use nohup so that any commands that are currently running won't quit due to quitting of the Terminal app.


This works for me:

#!/bin/sh

{your script here}

osascript -e 'tell application "Terminal" to close (every window whose name contains ".command")' &
exit

This will work for closing just your windows opened with a .command file but leave things already running in other terminal windows. I know that I almost always have either sass or grunt watch something so I don't want to quit terminal totally.