Bash and True Color
By Emma Payne •
I am trying to construct a simple bash prompt with true colors and have tried everything with this code as follows--don't see what is the problem and need info from you scripting gurus out there :)
#set your colors
pipe_color='ff;ff;ff'
pipe_bg_color='0;0;0'
username_color='0;0;0'
username_bg_color='c0;c5;ce'
at_color='ff;ff;ff'
at_bg_color='41;4a;4c'
host_color='ff;ff;ff'
host_bg_color='00;5b;96'
workingdir_color='ff;ff;ff'
workingdir_bg_color='36;80;2d'
# leave this block alone
pipe_color_set="\x1b[38;2;${pipe_color}m"
pipe_bg_color_set="\x1b[48;2;${pipe_bg_color}m"
username_color_set="\x1b[38;2;${username_color}m"
username_bg_color_set="\x1b[48;2;${username_bg_color}m"
at_color_set="\x1b[38;2;${at_color}m"
at_bg_color_set="\x1b[48;2;${at_bg_color}m"
host_color_set="\x1b[38;2;${host_color}m"
host_bg_color_set="\x1b[48;2;${host_bg_color}m"
workingdir_color_set="\x1b[38;2;${workingdir_color}m"
workingdir_bg_color_set="\x1b[48;2;${workingdir_bg_color}m"
color_reset_set='\x1b[0m'
# leave this block alone
pipe=$(printf "${pipe_color_set}")
pipebg=$(printf "${pipe_bg_color_set}")
username=$(printf "${username_color_set}")
usernamebg=$(printf "${username_bg_color_set}")
at=$(printf "${at_color_set}")
atbg=$(printf "${at_bg_color_set}")
host=$(printf "${host_color_set}")
hostbg=$(printf "${host_bg_color_set}")
workingdir=$(printf "${workingdir_color_set}")
workingdirbg=$(printf "${workingdir_bg_color_set}")
colorreset=$(printf "${color_reset_set}")
# your PS1 prompt. configure as desired
export PS1='\[${pipe}${pipebg}\]|\[${username}${usernamebg}\][\u]\[${pipe}${pipebg}\]|\[${at}${atbg}\]@\[${pipe}${pipebg}\]|\[${host}${hostbg}\][\h]\[${pipe}${pipebg}\]|\[${colorreset}\] ' 1 Answer
ANSI color codes use decimal, not hex.
For example, white is 255;255;255 (that is \e[38;2;255;255;255m) and your "working directory background" dark green is 54;128;45 (that is \e[38;2;54;128;45m).
Also: Don't bother with printf here – you can directly put escape codes such as \x1b or \e in PS1 and bash itself will automatically expand them when displaying the prompt. (In your case, replace the single quotes of PS1 with double quotes.)
pipe_color='255;255;255'
pipe_bg_color='0;0;0'
username_color='0;0;0'
username_bg_color='192;197;206'
at_color='255;255;255'
at_bg_color='65;74;76'
host_color='255;255;255'
host_bg_color='0;91;150'
workingdir_color='255;255;255'
workingdir_bg_color='54;128;45'
pipe="\e[38;2;${pipe_color}m"
pipe_bg="\e[48;2;${pipe_bg_color}m"
username="\e[38;2;${username_color}m"
username_bg="\e[48;2;${username_bg_color}m"
at="\e[38;2;${at_color}m"
at_bg="\e[48;2;${at_bg_color}m"
host="\e[38;2;${host_color}m"
host_bg="\e[48;2;${host_bg_color}m"
workingdir="\e[38;2;${workingdir_color}m"
workingdir_bg="\e[48;2;${workingdir_bg_color}m"
color_reset='\e[0m'
PS1="\[${pipe}${pipe_bg}\]|\[${username}${username_bg}\][\u]\[${pipe}${pipe_bg}\]|\[${at}${at_bg}\]@\[${pipe}${pipe_bg}\]|\[${host}${host_bg}\][\h]\[${pipe}${pipe_bg}\]|\[${color_reset}\] " 1