How to change the title of the current terminal tab using only the command line [duplicate]

None of the currently posted answers works/answers the question.

As per my original question, neither setting PS1 nor PROMPT_COMMAND had any effect.


Using only a command at the command prompt, how do I change the title of the current terminal tab?

Many posts suggest this:

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

but it does nothing.

None of the current answers works (some don't answer the question), so for clarity:

  • Once the title is changed, I don't want it to change if I change directory etc
  • I don't want the same title on all tabs. I only want to set the title for the tab I run the command in
  • I want multiple tabs to each have different titles

Also, the PROMPT_COMMAND variable is not set in my terminal sessions. If I set it:

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

it has no effect.

What is the correct command?


FYI, the output of uname -a is:

Linux d136172 3.13.0-45-generic #74-Ubuntu SMP Tue Jan 13 19:36:28 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux


Solution 1:

from @Maythux, this one works for my needs to disregard my auto-prompt current-directory on terminal.

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

Instruction

Change the string on "New Terminal Name" with $("pwd"):

PROMPT_COMMAND='echo -en "\033]0; $("pwd") \a"'

This will automatically change the title even when you add a new tab.


I use the setting below which looks better, you can also play bash programming and set your own.

PROMPT_COMMAND='echo -en "\033]0;$(whoami)@$(hostname)|$(pwd|cut -d "/" -f 4-100)\a"'

Add this setting to your ~/.bashrc.

Solution 2:

From https://askubuntu.com/a/774543/455406, a bash-specific solution is to create a custom function (see e.g. this how-to) like

# function to set terminal title  
function set-title() {
  if [[ -z "$ORIG" ]]; then
    ORIG=$PS1
  fi
  TITLE="\[\e]2;$*\a\]"
  PS1=${ORIG}${TITLE}
}

which allows you to call set-title <name you want to set it to>

Solution 3:

It is very likely that PROMPT_COMMAND is set and it is overwriting your choice of title every time the prompt is displayed. Try unsetting it and then issuing your title command:

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