Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

You must log in to answer this question.

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