Color in Bash Prompt messing up the prompt

5

In my bashrc file I have my prompt set as follows:

  TC_GRE="^[[0;32;40m"                                                          
  TC_RESET="^[[0m"                                                              
  PS1="${TC_GRE}i:${TC_RESET}"  

The prompt therefore is simply a green "i:". When I work in my shell and I scroll up in the history my command line messes up. Consider the following:

i: shell_command_one
i: shell_command_two
i: shell_command_three

Now when I go up in my history the line might look something like this:

i: shell_comshell_command_two

If I hit enter on that it executes shell_command_two. (Notice how the shell_com is just junk characters on the terminal.

I suspect it might have something to do with the color characters being non-printing. Does anyone know how to fix this in bash?

p.s. I'm not sure if this is better posted on superuser but I thought it might be best here since its about bash scripting.

David Mokon Bond

Posted 2013-02-18T18:25:09.770

Reputation: 105

Answers

3

It appears you have a malformed CSI color code. Try this in your .bashrc file:

TC_GRE="\[\033[0;32m\]"                                                          
PS1="${TC_GRE}i: "  

emallove

Posted 2013-02-18T18:25:09.770

Reputation: 432

Worked with a small fix...

TC_GRE="[\033[0;32;40m]" TC_RESET="[\033[0;0m]" PS1="${TC_GRE}i:${TC_RESET}"

I still wanted to reset the foreground color so it wasn't always green. Thanks! – None – 2013-02-18T18:46:33.567

3It's not so much a malformed color code, as you were inserting characters into the prompt which did not move the cursor when printed. The \[...\] pair wraps those characters so that bash doesn't get confused about how much space on-screen the prompt occupies. – chepner – 2013-02-18T19:39:05.070