Close Terminal window from within shell script (Unix)?

MacosShellUnixCommand

Macos Problem Overview


Is there a way to close a Terminal window from within a shell script? I have a .command file that should just get out of the way once it's done.

Macos Solutions


Solution 1 - Macos

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.

Solution 2 - Macos

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")' &

Solution 3 - Macos

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.

Solution 4 - Macos

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.

Solution 5 - Macos

closeWindow() {
	/usr/bin/osascript << _OSACLOSE_
	tell application "Terminal"
		close (every window whose name contains "YourScriptName")
	end tell
	delay 0.3
	tell application "System Events" to click UI element "Close" of sheet 1 of window 1 of application process "Terminal"
_OSACLOSE_
}

This will close the Terminal window for your script and keep any other Terminal windows open as long as their window titles don't match. For it to work Terminal will have to be added to the list of applications permitted to use the Accessibility framework. You can also cycle through Terminal windows with a repeat command and close every window x that contains a UI element "Close" on sheet 1.

Solution 6 - Macos

I find the best solution for this is to use Automator to create a true OSX application which will work the same way regardless of how your system is configured. You can have the Automator run your shell script, or you can embed the shell script itself in Automator.

Here is how you do it:

  1. Run Automator (in Applications).

  2. Choose "New Document" and when it asks "Choose a type for your document" choose "Application"

  3. In the left panel, select "Utilities" then "Run Shell Script".

  4. Type in your script commands in the workflow item in the right panel. You can either call another shell script, or just put your commands in their directly.

  5. Save the Application, which will be a full-fledged Mac App. You can even cut-and-paste icons from other apps to give your script some personality.

Solution 7 - Macos

#!/bin/bash -x

{your script here}

. exit 0
kill -9 $PPID

you can also create a shortcut for your script:

cp yourscript.sh ~/bin/yourshortcutnamewhateveryouwant

then type

yourshortcutnamewhateveryouwant

will run whatever is writen into script at any directory.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionericsocoView Question on Stackoverflow
Solution 1 - MacosSimon UrbanekView Answer on Stackoverflow
Solution 2 - MacosKevinMView Answer on Stackoverflow
Solution 3 - Macosjaypal singhView Answer on Stackoverflow
Solution 4 - MacosArronView Answer on Stackoverflow
Solution 5 - Macosuser2373121View Answer on Stackoverflow
Solution 6 - MacosGary WisniewskiView Answer on Stackoverflow
Solution 7 - Macosozan paliView Answer on Stackoverflow