Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

My Bash Prompt is currently setting the xterm titlebar using the following sequence:

PS1='\033]0;\u@\h>\w\007'

Is there an easy way to display the current command in the titlebar. For example, if I am tailing a file using tail -f foo.log, I want my titlebar to say tail -f foo.log.

share|improve this question
add comment

2 Answers

up vote 8 down vote accepted

basically, you need:

trap 'echo -ne "\033]0;$BASH_COMMAND\007"' DEBUG

at the end of your .bashrc or similar. Took me a while to work this out -- see my answer here for more information :)

share|improve this answer
    
Thanks for this solution simon! However, when adding this to my .bashrc after each command in my shell I get this output 0;%s@%s:%s" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}. I assume that I can suppress this output by assigning the trap command to PS1? However, PS1=trap 'echo -ne "\033]0;$BASH_COMMAND\007"' DEBUG does not work. It leads to the error bash: echo -ne "\033]0;$BASH_COMMAND\007": command not found. –  orschiro Dec 7 '13 at 8:17
add comment

(Inspired by this SU answer)

You can combine a couple bash tricks:

  • If you trap a DEBUG signal, the handler is called before each command is executed
  • The variable $BASH_COMMAND holds the currently executing command

So, trap DEBUG and have the handler set the title to $BASH_COMMAND:

trap 'echo -ne "\033]0;$BASH_COMMAND\007"' DEBUG

This will keep the title changed until something else changes it, but as long as your $PS1 stays the same it won't be a problem -- you start a command, the DEBUG handler changes the titlebar, and when the command finishes bash draws a new prompt and resets your titlebar again.

A useful tip found here (also where that SU answer came from) is to include:

set -o functrace

This will make bash propagate the DEBUG trap to any subshells you start; otherwise the titlebar won't be changed in them

share|improve this answer
    
ha! same answer at exactly the same time! +1 for handling subshells, but do you have any answer to the pipes problem? (see my link to the SO question) –  simon Mar 10 '11 at 15:47
add comment

Your Answer

 
discard

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.