如何让终端的顶栏说出正在运行的命令?

我经常并行运行命令需要很长时间才能完成,有时我会忘记运行的内容,因为它们在屏幕上输出的信息基本相同。

你知道有什么方法可以找出哪个命令在哪个终端运行?

取自Bash – 通过运行第二个命令更新终端标题·U&L并略微更改:

trap 'echo -ne "\033]2;$(history 1 | sed "s/^[0-9 ]* \+//")\007"' DEBUG 

此(ab)使用DEBUG信号作为触发器,通过XTerm控制序列使用历史记录中的最后一个条目(即您执行的最后一个命令)更新标题。 将行添加到~/.bashrc以在每个新终端窗口中启用该function。

要在标题中打印其他命令输出,请说出当前目录,其中包含pwd后跟“:”和当前运行的命令,我建议使用printf ,如下所示:

 trap 'echo -ne "\033]2;$(printf "%s: %s" "$(pwd)" "$(history 1 | sed "s/^[0-9 ]* \+//")")\007"' DEBUG 

一些终端模拟器允许您指定动态标题,甚至可以将命令名称作为选项,这样您甚至不需要摆弄 – 我搜索并在yakuake的配置文件设置中找到它。

可以通过更改变量$PS1 (主要提示字符串)的值来更改终端窗口标题。 [1] [2] 。 我们可以将此解决方案与使用Dessert的答案中的history命令相结合。


方法1:自动更新$PS1的值。 (更新)

~/.bashrc添加到文件~/.bashrc的底部:

 # Change the terminal window title, based on the last executed command rtitle() { # If the variable $PS1_bak is unset, # then store the original value of $PS1 in $PS1_bak and chang $PS1 # else restore the value of $PS1 and unset @PS1_bak if [ -z "${PS1_bak}" ]; then PS1_bak=$PS1 PS1+='\e]2;$(history 1 | sed "s/^[0-9 ]* \+//")\a' else PS1=$PS1_bak unset PS1_bak fi }; export -f rtitle # Export the function to be accessible in sub shells #rtitle # Uncomment this line to change the default behaviour 

然后source ~/.bashrc或只是打开一个新的终端并以这种方式使用该function:

  • 执行rtitle ,根据上次执行的命令自动开始更改终端窗口标题。
  • 再次执行rtitle以返回默认行为。

方法2:手动更新$PS1的值。 (初答)

~/.bashrc添加到文件~/.bashrc的底部:

 set-title() { # Set a title of the current terminal window [[ -z ${@} ]] && TITLE="$(history 2 | head -1 | sed "s/^[0-9 ]* \+//")" || TITLE="$@" # If the title is not provided use the previous command [[ -z ${PS_ORIGINAL} ]] && PS_ORIGINAL="${PS1}" || PS_ORIGINAL="${PS_ORIGINAL}" # Use the original value of PS1 for each future change PS1="${PS_ORIGINAL}"'\e]2;'"$TITLE"'\a' # Change the prompt (the value of PS1) }; export -f set-title 

然后source ~/.bashrc或只是打开一个新的终端并以这种方式使用该function:

  • set-title 会将终端窗口标题更改为
  • 不带参数的set-title会将终端窗口标题更改为上一个命令。

参考和例子:

  • Ubuntu 15.04全新安装:无法重命名gnome-terminal选项卡
  • 如何在ubuntu 16.04中更改终端标题
  • 例1 ; 例2