9

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.

1 Answer 1

7

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

TC_GRE="\[\033[0;32m\]"                                                          
PS1="${TC_GRE}i: "  
3
  • 1
    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!
    – David Mokon Bond
    Commented Feb 18, 2013 at 18:46
  • 5
    It'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
    Commented Feb 18, 2013 at 19:39
  • One step towards understanding how these colour codes work. It is helpful to break them down....
    – justintime
    Commented Aug 1, 2021 at 17:46

You must log in to answer this question.

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