Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

While modifying the definition of my PS1, I saw that "\[" and "\]" markers should be added to help bash to compute the right display lenght. Many exemples on the web do not use them or even mention them.

I searched for a solution to add them automatically, like with sed, but I didn't find any example.

Are they still needed and is there a recommandation not to use sed to define PS1?

This is what I use at the moment:

PS1="$(echo -n "$PS_" | sed -e 's/\\e\[/\\\[\\e\[/g' -e 's/[0-9]m/&\\\]/g')"
share|improve this question
up vote 0 down vote accepted

Yes, \[ and \] still are needed, otherwise you'll run into ugly glitches when the command is wider than your terminal window.

share|improve this answer
That's what I have read for the escape chars, but what about the use of sed to define PS1? – PRouleau Aug 30 '11 at 13:42
The thing is, not all of them end with a 'm'... If you use the escapes purely for formatting, it's okay - it's your bashrc, do what you want. But it's not a very clean way. Many other users instead make variables like red='\[\e[31m\]' and then have PS1="${red}\u${norm} ..." Nice and clean. – grawity Aug 30 '11 at 13:50
Wheel, I also use variables to keep the definition easier to read, but all the .bash_color files I saw defined them without the "\[\]". So I am wondering. Thanks for your help. – PRouleau Sep 8 '11 at 0:27

Your Answer


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.