Mac OS X change the terminal window title
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.








des09 said on November 8, 2008
How would you set the tab title when running multiple tabs?
luca said on April 27, 2009
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 said on November 20, 2009
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 said on November 20, 2009
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 said on January 20, 2010
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.
Ivan Tumanov said on February 9, 2011
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 said on November 7, 2011
Awesome. Perfect. Thanks!
Pedro Furlanetto said on November 13, 2011
I found this one to be the most convenient:
export PS1=”\$(echo -n -e ‘33]0;\W07′)\W \u $ “
moonpixel said on November 30, 2011
nice, using it like this
#!/usr/bin/env bash
echo -n -e “33]0;$107″
exit 0