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.
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 thatbash
doesn't get confused about how much space on-screen the prompt occupies. – chepner – 2013-02-18T19:39:05.070