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 example, these work equally well for me:

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

I wonder what difference does it make?


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.

| improve this answer | |

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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