Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
1  
Please don't cross-post. –  Dennis Williamson Feb 21 '11 at 15:41

2 Answers 2

up vote 8 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.

share|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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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