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.


1 Answer 1


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‐
  • 2
    @theillien: not sure, but I think PS1='\[\e[0;32m[\h::\W] >>\e[\]m ' might not be right. Youu may still be off in the count though you likely won't notice until you start doing some prompt redraws - like for newlines in $PS2 or so. I think this is safer PS1='\[\e[0;32m\][\h::\W] >>\[\e[m\] ' - see how the \e bits are enclosed in the \[...\] quoted portion, but the printing bits - even the bash specific escapes are not enclosed. readline needs to know what bits are intercepted by the terminal - like color escapes - and what bits actually make it on the screen. Hence \[...\]
    – mikeserv
    Aug 16, 2014 at 4:51
  • I have seen the newline issue that you mentioned. Instead of continuing a command onto a new line it begins to overwrite the line on which it started. I've replaced it with your suggestion. We'll see if that makes a difference.
    – theillien
    Aug 25, 2014 at 23:06
  • What threw me off was the \[ is what needs to be closed with \] versus the [ which is part of the ANSI sequence. For example \e[31;1m for bright red. The ANSI sequence is within the bounding brackets \[ ... \] and its internal [ does NOT get a closing ] or \] bracket. Hopes this helps someone, even though it's 5 years later. (If I found it hopefully this will help someone else too.)
    – Nate
    Oct 18, 2019 at 21:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .