Change the title of a terminal on Ubuntu 16.04
Hello wayward traveler. This post actually describes a way to change the title of a gnome terminal running bash that works. It also discusses why many other online recommendations don't work.
Need help installing Ubuntu?
Just the command
into a gnome-terminal running an interactive bash shell to change the title to New title. This will also set the prompt to $.
There's a discussion about why you need to reset PS1 on Ubuntu 16.04 below.
bash 4.3.48(1)-release (x86_64-pc-linux-gnu)
Many, many, many online recommendations suggest you run:
...but this "doesn't work."
It doesn't work because Ubuntu 16.04 sets the PS1 variable to _also_ run this command. Since the PS1 variable is evaluated for each prompt, its setting overwrites the setting you set. This make you _think_ that its not working, but it is - its just getting overwritten.
The "PROMPT_COMMAND" solution
Some online resources [link] suggest you set PROMPT_COMMAND to something like:
This also "doesn't work" because PS1 is still set; PROMPT_COMMAND will run, then PS1 will run overriding what prompt command did.
This is especially insidious because as you debug, you may set PROMPT_COMMAND. If you figure out that PS1 is overriding your echo -en, then PROMPT_COMMAND will override it instead!
Why is everyone Googling this?
This is just speculation. Ubuntu 16.04 uses gnome-terminal 3.18.3. Support for gnome-terminal --title was not present in 3.18.3. Ubuntu 16.04 also ships with PS1 set to:
Notice the start (bolded here):
By default Ubuntu 16.04 runs the title set when PS1 is evaluated. As discussed above, this means that it will override any attempts to set the title with echo or PROMPT_COMMAND.
People, having lost --title then looked for another way to set the title, but their efforts were frustrated by the setting of PS1!
Why is this a big deal?
Many people have a lot of terminal windows open on their desktop. They give each terminal a title so that they can easily find it again. Without a title, people have to resort to looking through their windows manually which takes a lot of time.
The bigger issue
In the bug reports, many users were very upset about --title being removed.
One new Linux user wrote:
I've spent 15 years doing commercial development and minor admin in Windows, and the last year struggling with Linux, which is strangely obtuse and badly documented when it could be so simple and powerful for new users.
Why on earth have you taken away a simple command like title - which still exists and is incredibly useful in Windows, by the way - and then advise _your_ _users_ to learn some obtuse escape character code?
It would be good for Canonical to add a few test cases for this use case to make sure that people can easily set window titles from the command line.
bash(1) - Linux man page @ [link]
PROMPT_COMMAND @ [link]
ASCII Characters for MPE Users @ [link]
Xterm Control Sequences @ [link]
VT102 escape and control sequences @ [link]
Summary of ANSI standards for ASCII terminals Joe Smith, 18-May-84, With additions by Dennis German @ [link]
Everything You Need to Know About HTML’s ‘pre’ Element @ [link]
Ubuntu logo from [link]
To search for \[ when you type man bash you use /\\\[.
Octal and hex escape values in bash
\033 is an example of specify an octal backslash escape in bash, \x1b is how to specify the backslash escape in hex
Am I running an interactive shell?
You're running an interactive shell if PS1 is set and $- includes i.
ASCII escape sequences and bash
\[ starts a sequence of non-printing characters and \] ends it.
\e is the ASCII escape character: \033 or \x1b or decimal 27 or Control key ^[
\e starts an ANSI escape sequence to perform in-band signaling to control "the cursor location, color, and other options on video text terminals." [Wikipedia]
The start of a ANSI OSC – Operating System Control
Starts a control string for the operating system to use that is terminated by an ST
Here is a screen shot of the relevant passage in a page on the Xterm Control Sequences
From man gnome-terminal
GNOME Terminal is a terminal emulation application that you can use to perform the following actions:
Access a UNIX shell in the GNOME environment.
A shell is a program that interprets and executes the commands that you type at a command line prompt. When you start GNOME Terminal, the application starts the default shell that is specified in your system account. You can switch to a different shell at any time.
Run any application that is designed to run on VT102, VT220, and xterm terminals.
GNOME Terminal emulates the xterm program developed by the X Consortium. In turn, the xterm program emulates the DEC VT102 terminal and also supports the DEC VT220 escape sequences. An escape sequence is a series of characters that starts with the Esc character.
GNOME Terminal accepts all of the escape sequences that the VT102 and VT220 terminals use for functions such as positioning the cursor and clearing the screen.