Mac OS X Change the Terminal Window Title

The title of the Mac OS X terminal window can easily be changed. Changing the title can be useful when running a script or when using multiple terminal windows for different purposes in order to identify them easily when switching between applications and windows.


In the terminal window, from a bash prompt (the default shell) or in a bash shell script, use the following command to change the terminal window title to Tech-Recipes rules:

echo -n -e "\033]0;Tech-Recipes rules\007"

You can place (just about) any text in place of “Tech-Recipes rules” including the contents of a variable. Consider the following example:

name=`hostname`
echo -n -e "\033]0;$name\007"

This will change the title of the terminal to the hostname of the computer running the shell.

The Conversation

Follow the reactions below and share your own thoughts.

  • des09

    How would you set the tab title when running multiple tabs?

  • luca

    Thanks, works like a charm. However, it doesn’t change the title of a tab, just that of the main windows in which many tabs are contained – so it’s useless to be able to see at a glance which terminal is the one you’re looking for.

  • Anonymous

    Perfect, I put this in my .bashrc and the title changes on each tab as I login

    echo -n -e “33]0;`hostname`07″

    The only problem I have is that it doesn’t change back, when I get logged out, so I have an alias to reset it…

    alias sett=”echo -n -e “33]0;`hostname`07″

  • Anonymous

    I put this in my .bashrc
    echo -n -e “33]0;`hostname`07
    Then on each tab as I log into a different machine (It has to have that line in the .bashrc of the user/machine I’m logging into) it sets the title as I log in to the machine, so all the tabs are correct.
    And to recover on logout, I put this in the .bash_logout:

    prev=`who am i | sed -e “s/.*((.*))/1/”`
    if test ! x$prev = x
    then
    echo -n -e “33]0;$prev07″
    fi

    alias sett=”echo -n -e “33]0;`hostname`07″

    • Anonymous

      In bash, you can do this:

      PS1=”33]0;H07h:W u$ ”

      The PS1 prompt gets printed at every command (see ‘man bash’). Thus, the window title gets set at each display of the prompt — so if you log out of one shell, the prompt from the old shell is displayed and re-sets the window title.

      This method also gets you all the bash prompt expansions (see ‘man bash’, search for PROMPTING), like H which expands to the full hostname and h which expands to the hostname up to the first . and so on.

  • http://twitter.com/ivantumanov Ivan Tumanov

    This works in an interesting way –

    export PS1=”$(echo -n -e “33]0;w07″)w $”

    executes the echo command to set the tab title to w and then shows a regular w $ prompt.

  • Julie

    Awesome. Perfect. Thanks!

  • Pedro Furlanetto

    I found this one to be the most convenient:

    export PS1=”\$(echo -n -e ’33]0;\W07′)\W \u $ “

  • http://moonpixel.com moonpixel

    nice, using it like this
    #!/usr/bin/env bash
    echo -n -e “33]0;$107″
    exit 0