macos OSX - 如何在执行“退出”命令后自动关闭终端窗口。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5560167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OSX - How to auto Close Terminal window after the "exit" command executed.
提问by darksteel
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.
杀戮终端。它会结束这个过程,但会很突然。我不认为这是最好的主意。
呼叫退出。我已经更改了设置,因此退出会关闭终端。尽管如此,该应用程序仍然在 Dock 中显示为打开状态,并且没有执行我想要它执行的操作。
右键单击>退出。但是,这不是退出它的终端命令。
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.
那么现在,我应该退出和关闭终端的方式是什么?我听说过一些关于 osascript 的事情,但我不太确定。我想退出并关闭,以便终端不再作为窗口和进程打开。
回答by Duke
in Terminal.app
在 Terminal.app 中
Preferences > Profiles > (Select a Profile) > Shell.
首选项 > 配置文件 >(选择配置文件)> 壳。
on 'When the shell exits' chosen 'Close the window'
在“当外壳退出时”选择“关闭窗口”
回答by holygeek
How about the good old Command-Q?
好老的 Command-Q 怎么样?
回答by Blaise
回答by AmerllicA
Actually, you should set a config on your Terminal, when your Terminal is up press ?+,then you will see below screen:
实际上,您应该在终端上设置一个配置,当您的终端启动时,按?+,然后您将看到以下屏幕:
Then press shell tab and you will see below screen:
然后按 shell 选项卡,您将看到以下屏幕:
Now select Close if the shell exited cleanly
for When the shell exits.
现在选择Close if the shell exited cleanly
的外壳退出时。
By the above config each time with exit
command the Terminal will close but won't quit.
每次使用上述配置exit
命令终端将关闭但不会退出。
回答by icktoofay
You could use AppleScript through the osascript
command:
您可以通过以下osascript
命令使用 AppleScript :
osascript -e 'tell application "Terminal" to quit'
回答by GOTO 0
In a terminal window, you can type:
在终端窗口中,您可以键入:
kill -9 $(ps -p $PPID -o ppid=)
This will kill the Terminal application process, which is the parent of the parent of the current process, as seen by the kill command.
这将杀死终端应用程序进程,它是当前进程的父进程的父进程,如 kill 命令所见。
To close a Terminal window from within a running script, you need to go up one more level in the process hierarchy like this:
要从正在运行的脚本中关闭终端窗口,您需要在进程层次结构中再上一层,如下所示:
kill -9 $(ps -p $(ps -p $PPID -o ppid=) -o ppid=)
回答by kalafun
I 've been using ctrl + d
. It throws you out into the destination where You've started the sqlite3 command in the first place.
我一直在用ctrl + d
。它会将您带入您首先启动 sqlite3 命令的目的地。
回答by insanikov
osascript -e "tell application \"System Events\" to keystroke \"w\" using command down"
osascript -e "tell application \"System Events\" to keystroke \"w\" using command down"
This simulates a CMD+ wkeypress.
这模拟了CMD+w按键。
If you want Terminal to quit completely you can use:
osascript -e "tell application \"System Events\" to keystroke \"q\" using command down"
如果您希望终端完全退出,您可以使用:
osascript -e "tell application \"System Events\" to keystroke \"q\" using command down"
This doesn't give any errors and makes the Terminal stop cleanly.
这不会产生任何错误并使终端干净利落地停止。
回答by David P.
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
您还可以使用这个复杂的命令,它不会触发有关终止自己进程的警告: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'
此命令很长,因此您可以quit
在 bash 配置文件中定义别名(例如):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.
这将允许您简单地输入quit
终端而无需摆弄任何其他设置。
回答by John
Use the osascript
command in your code as icktoofay mentioned: osascript -e 'tell application "Terminal" to quit'
osascript
在您的代码中使用 icktoofay 提到的命令: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.
然后,打开终端首选项,转到设置 > 外壳,并将“关闭前提示:”设置为“从不”。终端现在应该完全退出(不要在你的 Dock 中保持打开状态)并在退出前忽略提示。如果您只打开了一个终端窗口并且该osascript
命令是您的最后一行代码,则它应该等待您之前运行的任何命令完成。
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 (run echo $PATH
to see where Terminal looks for scripts).
如果你将osascript
代码包装在一个 shell 脚本文件中,你可以用你给它的任何简洁的文件名来调用它——只要它在终端的搜索路径中(运行echo $PATH
以查看终端在哪里查找脚本)。