Search this site


Metadata

Articles

Projects

Presentations

I am the winner, take THAT ksh!

I've been trying off-and-on for some time now to get dynamically updating xterm titles in ksh. Sure, you could set your prompt to something like:

XTERM="^[]2;Yay for titles^G"
PS1=$XTERM'$HOST($PWD) >'

This works all good and dandy until you try to write something on the command line longer than what's displayable on a single row of text, you'll see that ksh thinks the end of your line is shorter than where it's at. This is due to the problem that your xterm title is still physically a part of your prompt. This was too annoying for me to bother dealing with since my prompt at a minimum is something around 20 characters.
So I gave up trying different ways, and today I had another idea. $() and `` doesn't return anything printed to stderr. We'll use that to our advantage!

PROMPT='$HOST($(/bin/pwd | sed -Ee "s,(/usr)?$HOME,~,")) [!] '$EXITCODE'$Z '
EXITCODE='$(A=$? && [ $A -ne 0 ] && echo "!!${A}!! ")'
XTERM= 
case $TERM in
  aterm|xterm|dtterm)
    XTERM='$(echo -n "\033]2;'$PROMPT'\007" 1>&2)'
    ;;
esac
export PS1=$XTERM$PROMPT

As you can see, the $(echo ...) has stdout redirected to stderr, and therefore our xterm title text gets completely ignored by PS1 when it's executed.
Huzzah!