Monday, July 07, 2008

Setting Terminal Title

Now that I'm spending a lot of time working on zones I've found myself needing to keep my desktop better organized so I can quickly find the zone and host I need amongst a slew of terminals. I like to keep things simple, so I went with a little shell script that sets the title of a window on demand. Here's what I ended up with:

if [ -x /bin/zonename ]; then
# if we are on a box that supports zones, include zone info in title
/bin/echo "\033]0;`/bin/hostname` [`/bin/zonename`]\007\c"
# handle non-zone platforms by omitting the zone name
/bin/echo "\033]0;`/bin/hostname`\007\c"

This will update the gnome-terminal, or xterm title bar with "hostname [zonename]" on a platform that supports zones (as determined by the presence and executable attribute of /bin/zonename). If a host does not have that executable available and executable (such as pre-Solaris 10) it will simply print the hostname.

True to traditional UNIX' abbreviated nature I named the script stt, short for "set terminal title" and placed in my $HOME/bin directory for convenience. Now when I log in to a host, if I'll be in there for a while I just type 'stt' and my window is properly adorned.

A simple extension of this script would be to include the function in a shell's profile and inject it into the PS1 variable so that it is executed after each command. This would allow the title to update dynamically with each command. Haven't messed with that approach yet as this has scratched my itch quite well.


Piotr said...

It's nice, but you didn't mention something very important - it works only in bash. Don't try it in sh or ksh.

Christopher Hubbell said...

Well, actually I'm using in ksh at the moment. I'm not a fan of using bash under Solaris because there's no privileged (RBAC) bash shell. Works like a charm under ksh.

Michael said...

here's my method.

in /etc/profile:
printf "\033]0;`uname -n`\007"

then I've got a ~/bin directory with scripts like:
/usr/bin/ssh $*
print -n "\033]0;`uname -n`\007"

put the first in all your system profiles, and the second on your workstation. when you log into a box, it sets the title, when you log out, after using the wrapper script for ssh (similar scripts for other login methods) it resets the title. less output than putting it in PS1.

you can use \033]1 to change the icon title, if you have icons (like in cde)