I also had in the past the problem of ANSI colour codes messing up with command line navigation; you need to put the ANSI codes around \[ \]
in order for the command shell to know how (not) to take them into account as part of the input string.
As in:
export PS1="\[\e[0;35m\][\u@\h \W]\$\[\e[m\] "
echo -e "\[\e[0;35m\]YOU ARE ON THE LIVE SERVER !!\[\e[0m\]"
Some explanation as to why the shell needs \[
and \]
: To draw the prompt in the correct positions in the character matrix of a terminal the shell needs to know the correct length of the prompt string which is the amount of printable characters, i. e. not control characters or character sequences.
However, the shell doesn't know which character sequences the terminal considers printable. Therefore one needs to provide hints to the shell to distinguish between printable and non-printable sequences, which is the purpose of \[
and \]
.