4

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?

1

2 Answers 2

8

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.

6

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.

You must log in to answer this question.

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