GNOME 终端 - 选项卡标题中的进程名称

adu*_*ity 9 linux terminal gnome

如何将当前正在运行的进程名称放入 GNOME 终端选项卡标题(或只有一个选项卡时的标题栏)?

- 更新 -

为了澄清,我希望在运行进程时更新选项卡标题,例如:

# title is currently "bash"
$ find / -name foo # while searching for foo, title is "find"
$ # title is once again "bash"
$ less /proc/cpuinfo # title changes to "less"
$ man ls # title changes to man
$ # title returns to "bash"
Run Code Online (Sandbox Code Playgroud)

Dav*_*llo 8

找到了。该站点对解决方案提供了很好的解释。
在你的 bashrc 中,它看起来像:

case "$TERM" in
xterm*|rxvt*)
    set -o functrace
    trap 'echo -ne "\e]0;$BASH_COMMAND\007"' DEBUG
    PS1="\e]0;\s\007$PS1"
    ;;
*)
    ;;
esac
Run Code Online (Sandbox Code Playgroud)

就我个人而言,我不认为我会将它添加到我的 bashrc 中,因为当所有 shell 启动时,DEBUG 与 trace 结合确实会抛出大量垃圾。如果你能接受它,它实际上确实有效。但是,它确实显示了整个命令,而不仅仅是第一个单词。

  • 谢谢你提出一个好问题。起初这看起来像一个简单的问题......在前 10 分钟后,它开始让我发疯!这就是为什么我认为这是一个很好的问题 - 听起来很简单,但迫使我更深入地了解 shell、终端和信号之间的交互。 (2认同)

小智 5

好吧,既然每个人似乎都知道 David Pashley 的解决方案,我有点惊讶我花了这么长时间才找到这个解决方案,因为它几乎一样古老。

这个解决方案实际上处理了 bash 完成垃圾邮件垃圾。

需要明确的是:我在这里除了研究什么也没做。所有功劳都归功于Marius Gedminas

这对我来说非常适合 Gnome-Terminal/Terminator

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"'

    # Show the currently running command in the terminal title:
    # http://www.davidpashley.com/articles/xterm-titles-with-bash.html
    show_command_in_title_bar()
    {
        case "$BASH_COMMAND" in
            *\033]0*)
                # The command is trying to set the title bar as well;
                # this is most likely the execution of $PROMPT_COMMAND.
                # In any case nested escapes confuse the terminal, so don't
                # output them.
                ;;
            *)
                echo -ne "\033]0;${USER}@${HOSTNAME}: ${BASH_COMMAND}\007"
                ;;
        esac
    }
    trap show_command_in_title_bar DEBUG
    ;;
*)
    ;;
esac
Run Code Online (Sandbox Code Playgroud)

这也是一个交叉帖子,因为我刚刚发现它并想分享,我认为它在这里也很有用。