巴什 - 通过运行第二个命令更新终端称号 [英] Bash - Update terminal title by running a second command

查看:71
本文介绍了巴什 - 通过运行第二个命令更新终端称号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的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 轻松了许多,但在接下来我将你使用的假设庆典,而且它是一个相当新版本(> 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%肯定它会与子shell的组合做,或者:)

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 环境变量都出来了可能的解决方案,因为他们只是执行< STRONG>在命令返回。

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.

庆典,我能想到的只有这样才能达到你想要的是(AB)使用DEBUG信号。所以这里有一个解决方案 - 在坚持到底这是你的的.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 "\033]0;%s\007" "${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 "\033]0;%s\007" "${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!

这篇关于巴什 - 通过运行第二个命令更新终端称号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆