I have defined in one file which I source from .bashrc, color constants.. like these..

txtgrn='\e[0;32m' # Green
txtylw='\e[0;33m' # Yellow
txtblu='\e[0;34m' # Blue
txtpur='\e[0;35m' # Purple

When I try to use them in my $PS1 variable I am getting strange results, in that everything looks fine till I press CTRL-R for recursive history search when few characters on the begining scramble, so I am able for example to delete them pressing CTRL-D in shell behind that -> pointer :

I am using $'..' notation, to be able to insert unicode chars..

PS1=$'\xE2\x99\x94 [\\t][\u]->'  

so now, what is the proper way of inserting variable like $txtblu from above, into PS1 line..?


so line like this one:

PS1=$'\n\W\xe2\x98\x85 '$txtpur'\n\n[\t]'$txtrst'[\u]->'

makes rendering of symbols and colors ok, but when I press Ctrl+R, type few chars, and then Ctrl+B few(2 of them I think) characters appear on the beginning of resulting command search which are not belonging to that history line, and I am able to delete them.

  • just to add.. adding colors litterally like \\[\e[0;31m\\] works fine..
    – branquito
    Feb 11, 2014 at 2:20
  • like this for example PS1=$'\n\W\xe2\x98\x85 '$txtpur'\n\n[\t]'$txtrst'[\u]->'
    – branquito
    Feb 11, 2014 at 2:51
  • What version of bash do you have (paste the output of echo $BASH_VERSION)? You probably need to put \[ before and \] after to tell the shell the color escape sequences don't move the cursor position.
    – Mikel
    Feb 11, 2014 at 3:50
  • @Mikel hi Mikel, it's 4.2.45(2)-release.. could you please give me an example on my line from above, how should I wrap that around my color variable? thanks.
    – branquito
    Feb 11, 2014 at 3:53

2 Answers 2


First, you need an extra line in your colors file defining the STOP, a way of resetting the colors back to default:

txstop='\e[0m' # reset 

This will be needed to reset the colors after each color change, otherwise everything you type will be colored. Now, the other issue is that bash variables need to be in double quotes or they won't be expanded. Since you're using the $' ' syntax, this will require some quote acrobatics. For example:

PS1=$'\xE2\x99\x94'"$txtblu[\\t]$txstop $txtgrn[\u]$txstop$txtpur->$txstop"  

That looks like this:

enter image description here

  • well, try now with your line to do CTRL-R to reverse-i-search, type few chars to search for some previous command, and then try CTRL-B, and you will see that your cursor goes past -> and it should not :)
    – branquito
    Feb 11, 2014 at 3:10
  • for reseting color i had var $txtrst just did not mention it in my question, anyway thanks for noticing ;)
    – branquito
    Feb 11, 2014 at 3:11
  • no, it does not, see my comment above, I am getting the same situation as with my own code..
    – branquito
    Feb 11, 2014 at 3:19
  • @branquito strange, it works fine on my system, I can't reproduce your issue.
    – terdon
    Feb 11, 2014 at 3:22

When using non-printing characters in PS1, you need to wrap them in \[ and \]. In your example like this:

PS1=$'\n\W\xe2\x98\x85 \['$txtpur'\]\n\n[\t]\['$txtrst'\][\u]->'

Explanation: In order to wrap around long commands correctly, bash computes the length of the prompt. However when the PS1 variable contains non-printing characters, the length is computed wrong. Use \[ and \] to indicate "everything between this has a length of 0".


  • thanks very much on this explanation, now it all makes sense.
    – branquito
    Feb 14, 2014 at 2:29

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.