Mac OS X change the terminal window title

Contributor Icon Contributed by macster Date Icon September 22, 2004  
Tag Icon Tagged: Apple Mac

The title of the Mac OS X terminal window can easily be changed. This can be useful when running a script or when using multiple terminal windows for different purposes to easily identify them 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. For 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.

Previous recipe | Next recipe |
 
  • 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.
  • CeriDavies
    Perfect, I put this in my .bashrc and the title changes on each tab as I login

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

    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 "\033]0;`hostname`\007"
  • CeriDavies
    I put this in my .bashrc
    echo -n -e "\033]0;`hostname`\007
    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 "\033]0;$prev\007"
    fi





    alias sett="echo -n -e "\033]0;`hostname`\007"
  • jbbatzel
    In bash, you can do this:

    PS1="\033]0;\H\007\h:\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.
blog comments powered by Disqus