I want to customize my bash prompt using ANSI escape codes and have noticed that in some howtos the codes are enclosed in \[ and \] while in others they aren't.

For exaple, these work equally well for me:

PS1='\e[0;32m foo '
PS1='\[\e[0;32m\] foo '

I wonder what difference does it make?

link|improve this question

1  
Please don't cross-post. – Dennis Williamson Feb 21 '11 at 15:41
feedback

2 Answers

up vote 5 down vote accepted

The difference is that in the ones without \[ and \], bash will be unable to track the real length of the prompt and various visual glitches will reveal themselves over time.

link|improve this answer
feedback

It's explained nicely on this page:

These sequences will tell bash that the enclosed characters don't take up any space on the line, which will allow word-wrapping to continue to work properly. Without them, you'll end up with a nice-looking prompt that will mess up the screen if you happen to type in a command that approaches the extreme right of the terminal.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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