neo33 neo33 - 5 months ago 14
Bash Question

How to change the color of my hostname in bash?

I would like to modify the color of some parts of my prompt, I would like that it displays the color of my username in Cyan which I searched that it matchs with \u then I would like to display the color of my hostname in purple which matchs with \h, in order to achieve this when I type:

echo $PS1


I get:

[\u@\h \W]\$


then I tried:

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


But I failed since it just colors everything of Purple,
I would like to know how to achieve why is not coloring the hostname, the color's list is the following:

Color Code
Black 0;30
Blue 0;34
Green 0;32
Cyan 0;36
Red 0;31
Purple 0;35
Brown 0;33
Blue 0;34
Green 0;32
Cyan 0;36
Red 0;31
Purple 0;35
Brown 0;33

Answer

Reset each colour change via \e[0m.

Use \[...\] around invisible parts of the prompt, otherwise history navigation will get confused about the line length:

PS1="[\[\e[0;35m\]\u\[\e[0m\]@\[\e[0;36m\]\h\[\e[0m\] \W]\$ "
Comments