Bash Prompt Coloring – Fix Carriage Return Issues in Linux

bashbashrcps1terminalubuntu 11.10

When I try to colorize the bash prompt like this:

export PS1="\e[0;31m[\u@\h \W]\$ \e[m "

it breaks the carriage return (instead of going to a new line when line is full, it starts in the same line and erases the input text).

If I remove the color sequence it works fine.
I use Ubuntu 11.10 and terminal (guake has the same bug too).

Best Answer

You must wrap all escape sequences between \[ and \] in order for 'readline' to correctly calculate the prompt's visible length. Without them, 'readline' thinks the prompt is longer than it is, and doesn't know when the text reaches the margin.

PS1="\[\e[;31m\][\u@\h \W]\$ \[\e[m\]"
Related Question