Macos – How to get a Mac “.command” file to automatically quit after running a shell script

commandmacosscriptingshell

In my shell script, my last lines are:

...
echo "$l" done
done

exit

I have Terminal preference set to "When the shell exits: Close the window". In all other cases, when I type "exit" or "logout", in Terminal, the window closes, but for this ".command" file (I can double-click on my shell script file, and the script runs), instead of closing the window, while the file's code says "exit", what shows on the screen is:

...
$l done
logout

[Process completed]

…and the window remains open. Does anyone know how to get a shell script to run, and then just automatically quit the Terminal window on completion?

Thanks!

Best Answer

I was finally able to track down an answer to this. Similar to cobbal's answer, it invokes AppleScript, but since it's the only window that I'd have open, and I want to run my script as a quick open-and-close operation, this more brutish approach, works great for me.

Within the ".command" script itself, "...add this line to your script at the end"

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

SOURCE: http://forums.macosxhints.com/archive/index.php/t-2538.html