对于我连接到的任何主机,是否可以将 Gnome 终端的标题设置为“user@host”?

Naf*_*Kay 23 bash ssh gnome-terminal window-title

我想将终端标题设置为user@host以便我可以轻松地从窗口标题中知道我连接到哪台机器。有没有办法从 SSH 或 GNOME 终端执行此操作?

Tri*_*onX 21

是的。这是使用 PS1 的 bash 示例,该示例应该与发行版无关:

具体来说,转义序列\[\e]0; __SOME_STUFF_HERE__ \a\]很有趣。为了更清晰,我已将其编辑为设置在单独的变量中。

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
        # We have color support; assume it's compliant with Ecma-48
        # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
        # a case would tend to support setf rather than setaf.)
        color_prompt=yes
    else
        color_prompt=
    fi
fi

TITLEBAR='\[\e]0;\u@\h\a\]'
# Same thing.. but with octal ASCII escape chars
#TITLEBAR='\[\033]2;\u@\h\007\]'

if [ "$color_prompt" = yes ]; then
    PS1="${TITLEBAR}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\W\[\033[00m\]\$ "
else
    PS1="${TITLEBAR}\u@\h:\W\$ "
fi
unset color_prompt force_color_prompt
Run Code Online (Sandbox Code Playgroud)

另请注意,根据您使用的终端程序和外壳程序,可以有多种设置 xterm 标题的方法。例如,如果您使用 KDE 的 Konsole,您可以通过转到Settings-> Configure Profiles-> Edit Profile->Tabs并设置Tab title formatRemote tab title format设置来覆盖标题设置。

Konsole 标题栏设置对话框

此外,您可能想查看: