2

One can update terminal titles using the following syntax:

echo -ne "\033]0;TITLE\a"

where \a can also be \007. For dynamically updating the window title (on process start), I put the following in my .bashrc (inspired by this answer, simplified):

MY_TRAP_DEBUG() {
    echo -ne "\033]0; ${BASH_COMMAND} \a"
}
trap MY_TRAP_DEBUG DEBUG

Works fine. However, now when I want to print colored output:

echo -e "\033[0;33m SOME YELLOW TEXT"

, the output contains the command and results in ascii salad: see screenshot below.

enter image description here

  1. Why?
  2. How do I fix this?
phil294
  • 905

1 Answers1

3

That's two questions:

  • why?

    escape sequences don't nest. The title-sequence starts with \033]0; and ends with \a or any other control sequence.

  • how do you fix this?

    You could sanitize the bash command by assigning that to a variable and using shell parameter substitution to remove escape characters (and \a ASCII BEL). Just to make it look nice, you should also remove the square brackets (either [ or ]) after an escape character, as well as the numeric parameters that may follow.

Someone may provide an example using BASH_REMATCH (set as a side-effect of regex-matching) and use that as the word in a ${parameter##word} substitution...

Thomas Dickey
  • 76,765
  • I ended up with something like newTerminalTitle="${BASH_COMMAND//\\/\\\\}". the nesting totally makes sense, I havent thought of it. – phil294 Jun 03 '17 at 14:14