Bash: Setting the title of your terminal tab

If your terminal has many tabs, you'll want to keep them organized. To change their title from the prompt, run this function:

function tab_title {
  if [ -z "$1" ]
  then
    title=${PWD##*/} # current directory
  else
    title=$1 # first param
  fi
  echo -n -e "\033]0;$title\007"
}

Put it into your ~/.bashrc to have it always available. Adjust to your needs.

Usage

$> tab_title
# title set to the current directory's name
$> tab_title new_title
# title set to "new_title"

Auto-setting the title

If you want your title to update each time you change the working directory, put this code after the function definition.

# auto-title
cd() { builtin cd "$@" && tab_title; }
pushd() { builtin pushd "$@" && tab_title; }
popd() { builtin popd "$@" && tab_title; }
tab_title
Dominik Schöler Over 10 years ago