Bash Prompt – Backspace Deletes Prompt Issue

bashprompt

I recently modified my Bash prompt via the $PS1 variable so that I could have color. It is in my .bashrc file:

PS1="\\[\e[0;32m[\h::\W] >>\e[m "

Now if I type something and then have to backspace to clear it the entire prompt disappears. If I hit enter a new one shows up.

Looking at this question regarding the same issue it would appear that I am missing a closing bracket. I don't know where it would go, though.

Best Answer

At first I thought that the backslashes would self-escape within the double-quotes and that was the problem, but, on second-thought, "\\[" is equivalent to '\[' so this is not the case - it would have worked that way.

But the real problem was that readline did not know how many characters had been drawn to the screen and how many were intercepted as terminal escapes. In fact, it likely thought no prompt had been printed at all because your prompt consisted of what was essentially an open-quoted string.

So, as I noted in the comment, you needed to close the sequence. The \[ means begin non-printing escape sequence in the prompt - it's so readline can keep track of how many chars are drawn on the screen. You also need to end it like:

PS1='\[non-printing terminal escapes here\]' 

man bash 2>/dev/null | grep '^ *\\\[' -A5

\[     begin  a  sequence  of   non-printing
       characters,  which  could  be used to
       embed  a  terminal  control  sequence
       into the prompt
\]     end  a sequence of non-printing char‐
       acters
Related Question