Bash - 通过运行第二个命令更新终端标题 [英] Bash - Update terminal title by running a second command
问题描述
在我的 Ubuntu 终端上,我经常运行长时间运行的程序.而且由于有很多这样的程序,我总是忘记哪个终端用于哪个程序,除非我浏览所有这些程序.所以我想找到一种方法,每当我运行命令时,将我的终端标题更新为程序名称.我不想手动完成.
On my terminal in Ubuntu, I often run programs which keep running for a long time. And since there are a lot of these programs, I keep forgetting which terminal is for which program, unless I tab through all of those. So I wanted to find a way to update my terminal title to the program name, whenever I run a command. I don't want to do it manually.
我使用 gnome-terminal,但答案不应该真正依赖于此.基本上,如果我能够运行第二个命令,那么我可以简单地使用 gconftool 命令来更新标题.所以我希望找到一种方法来捕获 bash 中的命令并在每个命令之后更新标题.我怎么做?
I use gnome-terminal, but answer shouldn't really depend on that. Basically, If I'm able to run a second command, then I can simply use gconftool command to update the title. So I was hoping to find a way to capture the command in bash and update the title after every command. How do I do that?
推荐答案
我有一些答案给你 :) 你是对的,使用 gnome-terminal 并不重要,但使用什么命令很重要你正在使用的外壳.这在 zsh
中要容易得多,但在接下来的内容中,我将假设您使用的是 bash
,并且它是一个相当新的版本 (> 3.1).
I have some answers for you :) You're right that it shouldn't matter that you're using gnome-terminal, but it does matter what command shell you're using. This is a lot easier in zsh
, but in what follows I'm going to assume you're using bash
, and that it's a fairly recent version (> 3.1).
首先:
哪个环境变量会包含当前的命令"?
Which environment variable would contain the current 'command'?
有一个环境变量或多或少你想要什么 - $BASH_COMMAND
.只有一个小问题,那就是它只会显示管道中的最后一个命令.我也不是 100% 确定它会用子外壳的组合做什么:)
There is an environment variable which has more-or-less what you want - $BASH_COMMAND
. There's only one small hitch, which is that it will only show you the last command in a pipe. I'm not 100% sure what it will do with combinations of subshells, either :)
所以我希望找到一种方法在 bash 中捕获命令并更新每个命令后的标题.
So I was hoping to find a way to capture the command in bash and update the title after every command.
我一直在考虑这个问题,现在我明白你想要做什么,我意识到真正的问题是你需要在每个命令之前更新标题.这意味着 $PROMPT_COMMAND
和 $PS1
环境变量是可能的解决方案,因为它们仅在命令返回之后执行.
I've been thinking about this, and now that I understand what you want to do, I realized the real problem is that you need to update the title before every command. This means that the $PROMPT_COMMAND
and $PS1
environment variables are out as possible solutions, since they're only executed after the command returns.
在 bash
中,我能想到的唯一方法是(ab)使用调试信号.所以这里有一个解决方案——把它放在你的 .bashrc
的末尾:
In bash
, the only way I can think of to achieve what you want is to (ab)use the DEBUG SIGNAL. So here's a solution -- stick this at the end of your .bashrc
:
trap 'printf " 33]0;%s 07" "${BASH_COMMAND//[^[:print:]]/}"' DEBUG
为了解决管道问题,我一直在搞这个:
To get around the problem with pipes, I've been messing around with this:
function settitle () {
export PREV_COMMAND=${PREV_COMMAND}${@}
printf " 33]0;%s 07" "${BASH_COMMAND//[^[:print:]]/}"
export PREV_COMMAND=${PREV_COMMAND}' | '
}
export PROMPT_COMMAND=${PROMPT_COMMAND}';export PREV_COMMAND=""'
trap 'settitle "$BASH_COMMAND"' DEBUG
但我不保证它是完美的!
but I don't promise it's perfect!
这篇关于Bash - 通过运行第二个命令更新终端标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!