bash 从 shellscript 设置屏幕标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1687642/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 18:39:41  来源:igfitidea点击:

Set screen-title from shellscript

bashtitlegnu-screenxterm

提问by Beerweasle

Is it possible to set the Screen Title using a shell script?

是否可以使用 shell 脚本设置屏幕标题?

I thought about something like sending the key commands ctrl+Ashift-ANameenter

我想过发送按键命令之类的事情ctrl+ Ashift-ANameenter

I searched for about an hour on how to emulate keystrokes in an shell script, but didn't find the answer.

我搜索了大约一个小时关于如何在 shell 脚本中模拟击键,但没有找到答案。

回答by Shirkrin

You can set the screen / xterm title using the following lines:

您可以使用以下几行设置 screen / xterm 标题:

#!/bin/bash

mytitle="Some title"
echo -e '3k'$mytitle'3\'

[UPDATE] - by request I'm also including the solution proposed by @Espo below:

[更新] - 根据要求,我还包括以下@Espo 提出的解决方案:

Depending on your xterm version or your linux distribution the line above may or may not work and you can try the xterm-defaults:

根据您的 xterm 版本或您的 linux 发行版,上面的行可能有效也可能无效,您可以尝试 xterm-defaults:

#!/bin/bash

mytitle="Some title"
echo -e '3]2;'$mytitle'
    echo -n "3]0;${USER}@${HOST}
    echo -n "3]0;${USER}@${HOST}
Host *
  PermitLocalCommand yes
  LocalCommand [ "$TERM" == 'screen' ] && echo -ne "3k%h3\" 
7"
7"
7'

For more on the details see: http://www.faqs.org/docs/Linux-mini/Xterm-Title.html#s3or refer to the answer by @Espo below.

有关详细信息的更多信息,请参阅:http: //www.faqs.org/docs/Linux-mini/Xterm-Title.html#s3或参考下面@Espo 的回答。

回答by Espo

From http://www.faqs.org/docs/Linux-mini/Xterm-Title.html#s3

来自http://www.faqs.org/docs/Linux-mini/Xterm-Title.html#s3

xterm escape sequences

Window and icon titles may be changed in a running xterm by using XTerm escape sequences. The following sequences are useful in this respect:

  • ESC]0;stringBEL-- Set icon name and window title to string
  • ESC]1;stringBEL-- Set icon name to string
  • ESC]2;stringBEL-- Set window title to string

where ESC is the escape character (\033), and BEL is the bell character (\007).

Printing one of these sequences within the xterm will cause the window or icon title to be changed.

Note: these sequences apply to most xterm derivatives, such as nxterm, color-xterm and rxvt. Other terminal types often use different escapes; see the appendix for examples. For the full list of xterm escape sequences see the file ctlseq2.txt, which comes with the xterm distribution, or xterm.seq, which comes with the rxvt distribution.

Printing the escape sequences

For information that is constant throughout the lifetime of this shell, such as host and username, it will suffice to simply echo the escape string in the shell rc file:

export PS1='you_favorite_PS1_here'
if [ "$TERM" == 'screen' ]; then
    export PS1=${PS1}'\[3k\h3\\]'
fi

should produce a title like username@hostname, assuming the shell variables $USER and $HOST are set correctly. The required options for echo may vary by shell (see examples below).

For information that may change during the shell's lifetime, such as current working directory, these escapes really need to be applied every time the prompt changes. This way the string is updated with every command you issue and can keep track of information such as current working directory, username, hostname, etc. Some shells provide special functions for this purpose, some don't and we have to insert the title sequences directly into the prompt string. This is illustrated in the next section.

xterm 转义序列

可以使用 XTerm 转义序列在正在运行的 xterm 中更改窗口和图标标题。以下序列在这方面很有用:

  • ESC]0;stringBEL-- 设置图标名称和窗口标题为字符串
  • ESC]1;stringBEL-- 设置图标名称为字符串
  • ESC]2;stringBEL-- 设置窗口标题为字符串

其中 ESC 是转义字符 (\033),而 BEL 是响铃字符 (\007)。

在 xterm 中打印这些序列之一将导致更改窗口或图标标题。

注意:这些序列适用于大多数 xterm 衍生物,例如 nxterm、color-xterm 和 rxvt。其他终端类型通常使用不同的转义;有关示例,请参见附录。有关 xterm 转义序列的完整列表,请参阅文件 ctlseq2.txt,它随 xterm 发行版一起提供,或 xterm.seq,它随 rxvt 发行版一起提供。

打印转义序列

对于在此 shell 的整个生命周期中不变的信息,例如主机和用户名,只需在 shell rc 文件中回显转义字符串就足够了:

caption always "%{= kY}%-w%{= Yk}%n %t%{-}%+w%{ kG} %-= @%H - %LD %d %LM - %c"

假设正确设置了 shell 变量 $USER 和 $HOST,应该会生成一个类似于 username@hostname 的标题。echo 所需的选项可能因外壳而异(请参见下面的示例)。

对于在 shell 生命周期内可能会更改的信息,例如当前工作目录,每次提示更改时都需要应用这些转义。这样,您发出的每个命令都会更新字符串,并且可以跟踪当前工作目录、用户名、主机名等信息。一些 shell 提供了用于此目的的特殊功能,有些则没有,我们必须插入标题序列直接进入提示字符串。这将在下一节中说明。

回答by Johnny Halfmoon

The following are other ways to script the renaming of screen titles:

以下是编写屏幕标题重命名脚本的其他方法:

Adding the following settings to .ssh/configsets the screen title automatically upon logging in to a system using SSH:

添加以下设置以.ssh/config在使用 SSH 登录系统时自动设置屏幕标题:

export PS1='\[\e]0;My Title\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w$ '

Instead of %h, which represents the hostname of the machine you are connecting with, you may use %n, which is the actual name / alias you used to connect to the machine.

代替%h, 代表您正在连接的机器的主机名,您可以使用%n,这是您用于连接到机器的实际名称/别名。

NOTE: You need OpenSSH >= v5.1 to be able to use the Localhost %n and %h parameters. Check out 'man ssh_config' for more info on LocalCommand.

注意:您需要 OpenSSH >= v5.1 才能使用 Localhost %n 和 %h 参数。有关 LocalCommand 的更多信息,请查看“man ssh_config”。

To automatically revert the title, back to that of the hostname of the localhost, after closing the SSH session, you can add an escape sequence to you prompt variable PS1in .bashrc:

要自动恢复标题,回到本地主机的主机名,关闭 SSH 会话后,您可以在提示变量PS1中添加转义序列.bashrc

export PS1='\[\e]0;$USER\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w$ '

These tricks are especially useful when using a .screenrcconfig that shows you in what screen 'tab' you are currently working. Add something like the following to .screenrcto get this working:

这些技巧在使用.screenrc显示您当前正在工作的屏幕“选项卡”中的配置时特别有用。添加类似以下内容以.screenrc使其正常工作:

export PS1='\[\e]0;`hostname`\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w$ '

回答by Seff

Try the below commands, no need to edit any file or configuration like ~/.bashrc, Can be used at runtime.

试试下面的命令,不需要编辑 ~/.bashrc 之类的任何文件或配置,可以在运行时使用。

Set static text as title: (My Title)

将静态文本设置为标题:(我的标题)

export PS1='\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w$ '

Set local/global variable as title: ($USER)

将本地/全局变量设置为标题:($USER)

set_screen_title ()
{
    echo -ne "\ek\e\"
}

Set command output as title: (hostname)

将命令输出设置为标题:(主机名)

screen -X title "new title"

Set to default (Revert back):

设置为默认值(还原):

ESC]0;stringBEL -- Set icon name and window title to string
ESC]1;stringBEL -- Set icon name to string
ESC]2;stringBEL -- Set window title to string

回答by Randy Proctor

PS1='\e]0;string\a'

回答by idbrii

You can also call screen and tell it to set a title:

您还可以调用 screen 并告诉它设置标题:

ssh() {
  echo -n -e "3k3\"
  /usr/bin/ssh "$@"
  echo -n -e "3k`hostname -s`3\"
}
echo -n -e "3k`hostname -s`3\"

If you're in a screen window, it will set that window's name. If you're not in screen, it will set the most recently opened window's name.

如果您在屏幕窗口中,它将设置该窗口的名称。如果您不在屏幕中,它将设置最近打开的窗口的名称。

回答by Steven Penny

To add to Espo's answer, the xterm escape sequences can also be applied to the Bash PS1variable

要添加到 Espo 的答案中,xterm 转义序列也可以应用于 BashPS1变量

 # add the following in your ~/.bashrc or ~/.bash_profile
 PROMPT_COMMAND='printf "3]0;%s@%s:%s
set-title() {
  ORIG==$PS1
  TITLE="\e];$@\a"
  PS1=${ORIG}${TITLE}
}
7" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'

Example

例子

##代码##

回答by mijhael3000

To enable automatic title updating when jumping around with ssh, add this to ~/.bashrc:

要在使用 ssh 跳转时启用自动标题更新,请将其添加到~/.bashrc

##代码##

See http://linuxepiphany.blogspot.com.ar/2010/05/good-screenrc-config-setup.html

http://linuxepiphany.blogspot.com.ar/2010/05/good-screenrc-config-setup.html

回答by Yordan Georgiev

##代码##

or even better copy the whole concept for customizing your bash configs between a lot of hosts from here

甚至更好地复制整个概念,以便从这里在许多主机之间自定义您的 bash 配置

回答by Flurin Arner

My solution to this problem was to create a bash script and add it to my ~/.bashrc file:

我对这个问题的解决方案是创建一个 bash 脚本并将其添加到我的 ~/.bashrc 文件中:

##代码##

Now when I'm in any bash shell session, I type "set-title desired_title" and it changes to "desired title". This works for multiple versions of Ubuntu, currently on Kinetic 16.04

现在,当我在任何 bash shell 会话中时,我键入“set-title desired_title”,它会更改为“所需的标题”。这适用于多个版本的 Ubuntu,目前在 Kinetic 16.04

I got this solution from here. I was looking for it again, couldn't find it and thought I'd post it here for anyone interested.

我从这里得到了这个解决方案。我又在找它,找不到它,我想我会把它贴在这里给任何感兴趣的人。