When the prompt has colors and other escape sequences without printing, you should surround them with white single brackets. Here is a simple example:
PS1='\[\033[38;5;1m\]some red text\[\033[0m\]\$ '
This will cause the prompt to display βred textβ in red and the dollar sign (or β#β) in the default color.
Here is another way to do the same:
red='\033[38;5;1m' none='\033[0m' PS1='\[$red\]some red text\[$none\]\$ '
By the way, to make it more portable and to have less complicated variable buildup:
red=$(tput setaf 1) none=$(tput sgr0)
For more information on settings, see man 5 terminfo .
Dennis williamson
source share