Ubuntu – how can I set the bash window title to the last command that ran

bash

I'd like the title of my terminal windows to show the last command that ran. It'd be handy for finding the terminal that's running Mongrel in Dev/Test/Prod etc (for testing rails apps).

I tried this code:

if [ "$SHELL" = '/bin/bash' ]
then
    case $TERM in
         rxvt|*term)
            set -o functrace
            trap 'echo -ne "\e]0;$BASH_COMMAND\007"' DEBUG
            #  export PS1="\e]0;$TERM\007$PS1"
         ;;
    esac
fi

At least, I think that's the code that I tried. It did work, but it caused some strange behavior, like window titles that would get stuck in a loop until I hit Ctrl C when changing directory to a symlink.

What's a reliable way to set my window title to the last command that was run?

Best Answer

You can also use the xtitle tool:

sudo apt-get install xtitle
lastcmd() { xtitle $(history 1 | cut -c8-); }
PROMPT_COMMAND=lastcmd