I see there are other answers which are pretty much heuristic. However, if you have more specific needs (as you may do in future), I have a script which may be helpful to you.
# "Colorize" the plain text.
#
# Usage:
#
# $ colorize "TEXT" COLOR ["STYLE"] [BACKGROUND]
#
# Notes:
# - STYLE may be either a single value or a space-delimited string
#
# Examples:
#
# $ colorize "Hey!" blue bold
# $ colorize "Yo!" red italic white
#
colorize() {
text="$1"
if [ "$color_support" = true ]
then
color="$2"
style=($3)
background="$4"
colors=(black red green yellow blue purple cyan white)
styles=(regular bold italic underline reverse)
sn=(0 1 3 4 7)
for n in {0..7}
do
[[ $color == ${colors[n]} ]] && color="3$n"
[[ $background == ${colors[n]} ]] && background="4$n"
for s in ${!style[@]}
do
[[ ${style[s]} == ${styles[n]} ]] && style[s]="${sn[n]}"
done
done
! [ -z $style ] && style="${style[*]};" && style=${style// /;}
! [ -z $background ] && background=";$background"
background+="m"
text="\e[$style$color$background$text\e[0m"
fi
echo "$text"
}
It offers bold, italic, underline and reverse text styles, aswell as the supported colors in bash. You can also export the function in case you don't want to add it to .bash_profile
directly.
Here is an example how you could use it for formatting the prompt (note the prompt requires a bit different syntax):
colorize_prompt() {
colorize $@ &>/dev/null
if [ "$color_support" = true ]
then
text="\[\e[$style$color$background\]$1\[\e[0m\]"
fi
echo $text
}
# Main prompt
PS1="$(colorize_prompt "火" purple bold) $(colorize_prompt "\w" blue bold) "
# Continuation prompt
PS2="$(colorize_prompt "|" cyan bold) "
The script is an exempt from my dotfiles.