Ubuntu – How to set the title of the active gnome-terminal from the command line?

command linegnome-terminal

Is there a way to set the gnome-terminal title from within the terminal itself without having to right click on the tab. Something like:

active-terminal --title "Foo"

There was a related question previously with an answer that almost lets you do this: How to change Gnome-Terminal title? but that doesn't set the gnome-terminal tab title only the window title.

Best Answer

The following will set the terminal's title to "New terminal title":

echo -en "\033]0;New terminal title\a"

You will probably also have to change the environment variable PS1, first though, otherwise your changes won't show up because it will reset the title after each command. The default .bashrc that ships with Ubuntu contains the following line:

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

... the "\e]0;" code tells it to write everything up to the "\a" in both the title and icon-name properties. You need to remove that and set it to something like this (i.e. without the \e]0; code):

PS1="${debian_chroot:+($debian_chroot)}\u@\h \w\a$ "

Then any changes that you make with the above echo command will change the terminal title. If you're going to use this a lot, you can throw it into a function in your ~/.bashrc file:

set_term_title(){
   echo -en "\033]0;$1\a"
}

Then you can just set the title to "kittens" from the command line by doing:

set_term_title kittens

(You have to restart bash though after editing .bashrc, for your changes to take effect)