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.
-
1Then why open one in the first place? – Ignacio Vazquez-Abrams Jan 10 '12 at 04:41
-
1Doesn't `exit -f` work for you? You might want to use `nohup` if you don't want your commands that are running to quit abruptly. – jaypal singh Jan 10 '12 at 04:43
-
@IgnacioVazquez-Abrams I run a script by double-clicking a .command file, the functionality is complete, end of operation. i don't need the Terminal window hanging around. What's confusing about this? – ericsoco Jan 11 '12 at 06:14
7 Answers
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.

- 7,539
- 2
- 46
- 42

- 13,842
- 45
- 45
-
I do have "lose if shell exited cleanly" set, however in a certain script I want the terminal to stay open until user closes it... anyway around that? – Jonny May 14 '13 at 02:37
-
Not directly (because `.command` adds `;exit` to the shell), but you call an AppleScript that doesn't close, e.g.: `osascript -e 'tell application "Terminal"' -e activate -e 'do script "run.my.jobs"' -e 'end tell'` -- that window will always stay open – Simon Urbanek May 24 '13 at 14:29
-
Very cool. Thanks @SimonUrbanek. Getting my feet wet in the Appleverse and this tip is helping me automate some chores. – Preston Oct 14 '13 at 18:48
-
Works, but note that in Yosemite this preference is under Terminal -> Preferences -> Profiles, then click on Shell tab. – mrzzmr Jun 21 '15 at 19:45
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")' &
-
Still asks for confirmation to close it unless you change Terminal pref. – Damasio Nov 30 '16 at 18:18
-
7@damasio If you change the end from `&` to `& exit` at the end there will be no confirmation dialog – Chris Stratton Jan 04 '17 at 18:53
-
@KevinM is it possible to close a specific tab rather than a whole window? – RobertMcReed May 30 '19 at 21:29
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
.

- 74,723
- 23
- 102
- 147
-
+1 I don't have a Mac so I'm going to assume this is the correct answer. – SiegeX Jan 10 '12 at 05:02
-
@SiegeX LOL .. I knew we could run `apple scripts` from terminal. Didn't really knew it was the `osascript` command. However, there is another cool command called `killall` which kinda does the same. Thank you for your trust and upvote. Appreciate it. :-) – jaypal singh Jan 10 '12 at 05:06
-
3... wow - this is really radical - killing all open scripts is certainly not what I'd recommend. Note that the behavior is user-configurable so if you change your setting in *Settings* -> *Shell* -> *When the shell exists* to a more sane "Close if shell existed cleanly" then you simply need `exit 0` (or nothing) really. – Simon Urbanek Jan 10 '12 at 18:13
-
@SimonUrbanek didn't realize Terminal had that config option. That works perfectly. Your comment is really a separate answer on its own -- I went ahead and wrote it up as an answer below. Feel free to copy and paste it under your name if you want. Thanks! – ericsoco Jan 11 '12 at 06:27
-
killall Terminal will kill all the terminal windows. Not at all user friendly option. – Kaushik Wavhal Sep 24 '18 at 06:13
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.

- 767
- 8
- 11
-
2Variation for when you'd also like Terminal to quit, but only if there's no more open windows. `osascript -e 'tell app "Terminal"' -e 'close (every window whose name contains ".command")' -e 'if number of windows = 0 then quit' -e 'end tell' & exit;`. Multiple `-e`s add line breaks in the script; `"\n"` has issues for me. – Beejor Nov 22 '17 at 11:26
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.

- 81
- 1
- 1
-
-
Paste it inside your Bash script and call the **closeWindow** function when your script is completed. The function must be pasted into the script before it is called. – user2373121 Feb 02 '14 at 23:56
-
Had to activate Terminal as Accessibility for this. Feels a little like a security vulnerability... But works fine now thanks! – trevore Aug 26 '16 at 20:18
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:
- Run Automator (in Applications).
- Choose "New Document" and when it asks "Choose a type for your document" choose "Application"
- In the left panel, select "Utilities" then "Run Shell Script".
- 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.
- 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.

- 1,080
- 10
- 9
-
Upvoted. Very useful and swift solution in my case (mac user). – Brice Coustillas Oct 06 '19 at 09:48
#!/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.

- 31
- 5