I followed unix s.e post and added the line,

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

to my .bashrc file to set my last command as the terminal title. It works good on my ubuntu desktop machine.

When i add this line to my Centos server's .bashrc file and ssh into the Centos machine, it no longer works. SSH client terminal title has become blank and it prints all junk stuff on my console after every command,

Last login: Sun Jun  7 21:28:29 2015 from sk-box
]2;printf "k%s@%s:%s]0;printf "k%s@%s:%s" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"[root@sk-vbox ~]# cd
]2;printf "k%s@%s:%s]0;printf "k%s@%s:%s" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"[root@sk-vbox ~]# pwd
]2;printf "k%s@%s:%s]0;printf "k%s@%s:%s" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"[root@sk-vbox ~]# 

Is it possible to ensure that dynamic title works well over ssh?


1 Answer 1

PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'
trap 'echo -ne "\033]0;$BASH_COMMAND\007"' DEBUG

These two commands interfere with each other. BASH_COMMAND ends up containing the PROMPT_COMMAND, not the previously executed command. Since PROMPT_COMMAND contains a \007 (a bell character after backslash expansion), which is the end marker for the title text, the result is quite messy — you see the string after that from PROMPT_COMMAND before your prompt, and the bell rings when the second \007 is printed. You're also performing an extra level of backslash expansion in BASH_COMMAND.

Replace your DEBUG trap by a more robust one that takes care of not printing special characters:

trap 'printf "\033]0;%s\007" "${BASH_COMMAND//[^[:print:]]/}"' DEBUG
  • 1
    i would never have cracked this in my life, if it is not for you, Gilles....
    – user93868
    Jun 8, 2015 at 1:50

You must log in to answer this question.