1

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
/root
]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?

  • SSH has no impact here. The problem could be something conflicting in your .bashrc or an old bash version that doesn't have a feature you rely on. What version of bash is the server running? What else is in your .bashrc? – Gilles 'SO- stop being evil' Jun 07 '15 at 23:49
  • thanks Gilles. my .bashrc & /etc/bashrc/ files are there at - https://github.com/madhavan020985/local-rc/tree/master/centos-7. I am suspicious, if this line that sets the PROMPT_STRING - https://github.com/madhavan020985/local-rc/blob/master/centos-7/etc-bashrc#L22 and if i changed it by mistake. –  Jun 08 '15 at 00:52
  • and bash version is - 4.2.46 –  Jun 08 '15 at 01:03

1 Answers1

0
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.... –  Jun 08 '15 at 01:50