This question already has an answer here:

I wish to set a custom color for the hostname portion of my command line prompt (in bash).

In my .bashrc file, the default command line prompt is:

PS1='[\u@\h \W]\$ '

To colorize the hostname only, I came up with this prompt:

PS1='[\u@\e[40;36m\h\e[0m \W]\$ '

I used different colors for each host I work on. The colorizing part seems to be working as expected. However, with the prompt above, navigation on the command line becomes dysfunctional. For example, at times I cannot move the cursor to the beginning or end of the line. If I switch back to the default prompt, everything works correctly again.

What's wrong with my colorized prompt? How can I properly specify a color for the hostname only?

It should not matter for this question, but I'm running Arch Linux KDE and I work in Konsole.

marked as duplicate by Mikel, Community May 8 at 1:38

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

up vote 2 down vote accepted

Background

After the expansion is done for special prompt sequences, every character remaining in the prompt is counted in order to calculate the length of the prompt.

Problem

Since you've added the color sequences, which in fact shouldn't be counted for the length of a prompt, bash now actually thinks that your prompt is longer than it really is.
Because of that, bash can't know where is the beginning or the end of the line and that creates mentioned dysfunctionalities.

Solution

From bash manual:

\[    Begin a sequence of non-printing characters, which could be used to 
      embed a terminal control sequence into the prompt

\]    End a sequence of non-printing characters

Just add these around your color escape sequences so that they aren't counted for the prompt length.

Not the answer you're looking for? Browse other questions tagged or ask your own question.