Automatically close Terminal after script execution
10,783
Let's assume you call this script code you posted from within another shell. If you want the Terminal.app to be quit, you can use a simple AppleScript command:
osascript -e 'tell application "Terminal" to quit'
The same goes for iTerm2, or any other application. Just change its name in "Terminal"
. Or, to quit the frontmost app, as @Lri mentions:
osascript -e 'quit app (path to frontmost application as text)'
If your script instead is run non-interactively, then it should suffice to exit
it. The terminal window will not close though unless you change the terminal's settings:
Related videos on Youtube
Author by
Chad Daniels
Husband, dad and boss by day, dreamer by night, geek all the way. Follow the white rabbit for more (or shoot me a quick “Oh my paws and whiskers” on Twitter).
Updated on September 18, 2022Comments
-
Chad Daniels over 1 year
How do I automatically close the Terminal after script execution?
#!/bin/bash cd ~/Desktop/sh-scripts/ ./start.sh & // ??? how to close Terminal
-
Chad Daniels almost 12 yearsAnd if you just need the current session’s tab / window to be closed,
exit
will do just fine. -
slhck almost 12 years@kopischke No, that'll just exit the script being run. The terminal tab / window containing the shell the script was invoked in will stay.
-
Chad Daniels almost 12 years@slhck: ah, my bad – this needs to be set in the preferences for your session in Terminal.app. Default is, as you correctly stated, to not close the window – I changed that and forgot about it. So:
exit
will also close the terminal window / tab if your “Shell → On exit” session preference in Terminal.app is set to “Close window” or “Close window if no error occurred”. -
slhck almost 12 yearsThe OP runs a script though. Try it: create a shell script, then add
exit
at the end, and run that script from a terminal window. The script exits but the window stays @kop -
Chad Daniels almost 12 years@slhck: I did, closes it on mine, but that is because I set it to do so. Also: synchronous commenting :) – see my revised comment.
-
slhck almost 12 yearsNo I meant something different. If you open a terminal window and call a shell script from Bash, and that script exits, Bash won't exit, and so won't the terminal @kop
-
Chad Daniels almost 12 years@slhck: if you background the script, as OP does in his code, you are free to
exit
afterwards. Tested exactly that :). -
slhck almost 12 yearsBut if you run that piece of code starting with the hashbang from another shell, you will exit that tiny script if you type exit. Not the shell that started it. I'm not talking about the script.sh & line here — this is irrelevant. Will add an example layer. @kop
-
Chad Daniels almost 12 years@slhck: you are right – I’m assuming this is run interactively (yeah, I know the hashbang line does’t make any sense in that context – but neither does asking about closing Terminal when running a
.sh
script file directly, and why go to the trouble of having a shell script file that does nothing but launch another?). Anyway, never mind, your solution works fine and independently of session settings, so +1 as far as I’m concerned. -
slhck almost 12 yearsI have no idea about the context here so I guess you're right that a simple exit will do if the terminal is set up correctly :) @kop