When I'm done with Terminal, I want to exit it. Right now, I have three options:
killall Terminal. It will end the process, but rather abruptly. I don't think this is the best idea.
Call exit. I've changed the settings so exit closes Terminal. The app still appears open in the Dock though, and doesn't do what I want it to do.
Right click>Quit. However, this isn't a Terminal command to exit it.
So now, what is the way I should exit and close Terminal? I've heard something about osascript but I'm not too sure. I want to exit and close, so that Terminal is no longer open, both as a window and as a process.
Create a script:
cat ~/exit.scpt
like this:
command + w
to close the tab)tell application "Terminal" set WindowNum to get window count if WindowNum = 1 then quit else tell application "System Events" to keystroke "w" using command down end if end tell
Then add a alias in your *shrc
just like
vi ~/.bashrc
orzshrc
(anything else?)add it:
alias exit="osascript ~/exit.scpt"
And source the ~/.bashrc or reopen your terminal.app
Use the
osascript
command in your code as icktoofay mentioned:osascript -e 'tell application "Terminal" to quit'
Then, open Terminal preferences, go to Settings > Shell, and set "Prompt before closing:" to "Never." Terminal should now quit completely (not remain open in your dock) and ignore the prompt before quitting. If you have only one Terminal window open and the
osascript
command is your last line of code, it should wait for whatever command you ran before to finish.This would not be ideal if you are running scripts in the same window or other windows in the background (for instance, you may run a command in the background and continue using the current window for other commands if the first command is followed by an ampersand); be careful!
If you wrap the
osascript
code in a shell script file, you can probably call it with whatever pithy file-name you give it---as long as it is in Terminal's search path (runecho $PATH
to see where Terminal looks for scripts).I 've been using
ctrl + d
. It throws you out into the destination where You've started the sqlite3 command in the first place.You could use AppleScript through the
osascript
command:You can also use this convoluted command, which does not trigger a warning about terminating its own process:
osascript -e "do shell script \"osascript -e \\\"tell application \\\\\\\"Terminal\\\\\\\" to quit\\\" &> /dev/null &\""; exit
This command is quite long, so you could define an alias (such as
quit
) in your bash profile:alias quit='osascript -e "do shell script \"osascript -e \\\"tell application \\\\\\\"Terminal\\\\\\\" to quit\\\" &> /dev/null &\""; exit'
This would allow you to simply type
quit
into terminal without having to fiddle with any other settings.in Terminal.app
Preferences > Profiles > (Select a Profile) > Shell.
on 'When the shell exits' chosen 'Close the window'