Bash - Update terminal title by running a second command

后端 未结 8 1520
Happy的楠姐
Happy的楠姐 2020-12-24 13:27

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

8条回答
  •  被撕碎了的回忆
    2020-12-24 13:37

    I'm doing something like this, to show my pwd in the title, which could be modified to do whatever you want to do with the title:

    function title { echo -en "\033]2;$1\007"; }
    function cd { dir=$1; if [ -z "$dir" ]; then dir=~; fi; builtin cd "$dir" && title `pwd`; }
    

    I just threw this in my ~/.bash_aliases.

    Update

    I ran into strange bugs with my original answer. I ended up picking apart the default Ubuntu PS1 and breaking it into parts only to realize one of the parts was the title:

    # simple prompt
    COLOR_YELLOW_BOLD="\[\033[1;33m\]"
    COLOR_DEFAULT="\[\033[0m\]"
    TITLE="\[\e]0;\u@\h:\w\a\]"
    PROMPT="\w\n$ "
    HUH="${debian_chroot:+($debian_chroot)}"
    PS1="${COLOR_YELLOW_BOLD}${TITLE}${HUH}${PROMPT}${COLOR_DEFAULT}"
    

    Without breaking into variables, it would look like this:

    PS1="\[\033[1;33m\]\[\e]0;\u@\h:\w\a\]${debian_chroot:+($debian_chroot)}\w\n$ \[\033[0m\]"
    

提交回复
热议问题