Setting the gnome-terminal title in bash
2/9/2011There's a lot of confusion about how to set a title for the terminal tab, particularly using gnome-terminal.
Firstly, gnome-terminal must be told to allow changing the title. That's set in Edit -> Profile Preferences on the "Title and Command" tab. I use "replace initial title" because I typically have a lot of tabs open and it's far too long otherwise.
Next, bash uses a environment variable called PROMPT_COMMAND that updates the window title every time the prompt is displayed. Many times people seem to be setting the title successfully, but then the PROMPT_COMMAND is run and it resets their changes.
Anyway, I want to set the title to a custom variable, say TITLE, if present. Otherwise, I still want to have the username@host:pwd format. To accomplish that, I have added this to my .bashrc:
PROMPT_COMMAND=/home/mike/bin/prompt_command
function title {
export TITLE="$*"
}
With that I can run my own script. Here's an example script:
#!/bin/bash
if [ "x$TITLE" = "x" ]; then
echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"
else
echo -ne "\033]0;${TITLE}: ${PWD}\007"
fi
After setting that, I can simply run title some short description
to help
remind me what that tab was doing.