Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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' ]
    case $TERM in
            set -o functrace
            trap 'echo -ne "\e]0;$BASH_COMMAND\007"' DEBUG
            #  export PS1="\e]0;$TERM\007$PS1"

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?

share|improve this question
up vote 1 down vote accepted

You can also use the xtitle tool:

sudo apt-get install xtitle
lastcmd() { xtitle $(history 1 | cut -c8-); }
share|improve this answer

Hope this works for you:

lastcmd() { LASTCMD=$(history 1 | cut -c8-); echo -ne "\e]2;$LASTCMD\a\e]1;$LASTCMD\a"; }
share|improve this answer
So I'd put this in my .bashrc, correct? – jcollum Jan 17 '12 at 17:14
Nope, I added that to my bashrc and started a new terminal and I got this at my prompt: ]0;echo -ne ""]2;reload]1;reload~> and my window title is cut -c8-. That's what it looks like when I start the terminal. Sorry, that didn't work. – jcollum Jan 17 '12 at 17:18
Correct: ~/.bashrc is a good place to put that. Hummm... this works fine for me on Lubuntu10.10/bash at home and on my PuTTY/bash at work. Please can you provide a portion of your ~/.bashrc and $PS1, $PS2, $PS3 and $PS4? (you can edit your question). Then, I can test on my computer. Cheers. – olibre Jan 17 '12 at 21:43
@jcollum I can test on my computer, but I need to be as close as possible to your environement. Therefore, please provide some info as your .bashrc and PSx variables. Cheers. – olibre Jan 18 '12 at 21:11

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.