This question already has an answer here:

I want the name of command running , e.g unzip to be visible through the title bar of gnome-terminal , but it seems to be impossible , if the application running doesn't set a title explicitly , even though I choose 'Replace initial title' option in the profile dialog.

marked as duplicate by karel, pa4080, dessert, Videonauth, Eric Carvalho Dec 7 '17 at 14:17

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


This is a more complete solution actually taking care of bash-completion spamming garbage.

To be clear: I did nothing on my own here but research. All credit goes to Marius Gedminas.

This works perfectly for me with Gnome-Terminal/Terminator (put it in your .bashrc or somewhere that's getting sourced)

# If this is an xterm set the title to user@host:dir
case "$TERM" in
    PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"'

    # Show the currently running command in the terminal title:
    # http://www.davidpashley.com/articles/xterm-titles-with-bash.html
        case "$BASH_COMMAND" in
                # The command is trying to set the title bar as well;
                # this is most likely the execution of $PROMPT_COMMAND.
                # In any case nested escapes confuse the terminal, so don't
                # output them.
                echo -ne "\033]0;${USER}@${HOSTNAME}: ${BASH_COMMAND}\007"
    trap show_command_in_title_bar DEBUG

Also this is a cross-post because I just found out about it and wanted to share and I think it's useful here as well.

  • Don't do this without guarding the trap with a test like [ -n "$PS1" ] or your escape sequences will baffle scripts! – Martin Dorey May 12 '16 at 5:22
  • I've found using local this_command=$(HISTTIMEFORMAT= history 1 | sed -e "s/^[ ]*[0-9]*[ ]*//"); instead of $BASH_COMMAND cleaner as it shows aliases in their original unexpanded form. – zeroimpl Nov 22 '16 at 22:12
  • Hmm... I'm going to try to get this to display the vim buffer if vim is the command. – Costa Nov 18 '18 at 21:10

This has been sort of answered here.

  • trap 'command' DEBUG makes bash run command before every command.
  • echo -ne "\033]0;Title\007" changes the title to "Title"
  • $BASH_COMMAND contains the command being run.

Combining these we get

trap 'echo -ne "\033]0;$BASH_COMMAND\007" > /dev/stderr' DEBUG

Then we just have to reset the title after we complete the command. I did this by setting $PS1 to change the title to the current path.

tl;dr: Add these two lines (in this order, otherwise I got a garbled prompt) to the bottom of ~/.bashrc

trap 'echo -ne "\033]0;$BASH_COMMAND\007" > /dev/stderr' DEBUG

Edit: Your $PS1 might already change the title, in which case only the last line is needed.

  • 1
    Why the redirection to stderr? – wjandrea Dec 5 '17 at 21:13

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