colors in terminals
escape in ascii is 27 (decimal), \033 (octal), \x1b (hex).
not all shells seem to support all of it, f.e. in my case hex doesn't work.
some shells support \e. escape followed by a [ introduces a csi sequence,
ended by m the csi sequence changes character appearance.
\e[xm while x is semicolon seperated list of:
0 normal (default), 1 bold ...
30-37 foreground colors
40-47 background colors
to get proper line breaks notify shell that chars are non-printable (zero width)
by wrapping them in \[ and \].
examples
normal green: echo -e "\033[0;32mnormal green"
PS1 with bold red prompt, default white stdin and working linebreak:
PS1='\[\033[1;31m\]\$\[\033[0;37m\] '
rainbow:
for (( i = 30; i < 38; i++ )); do
echo -e "\033[0;"$i"m Normal: (0;$i); \033[1;"$i"m Light: (1;$i)";
done
table, taken from manjaros .bashrc:
colors() {
local fgc bgc vals seq0
printf "Color escapes are %s\n" '\e[${value};...;${value}m'
printf "Values 30..37 are \e[33mforeground colors\e[m\n"
printf "Values 40..47 are \e[43mbackground colors\e[m\n"
printf "Value 1 gives a \e[1mbold-faced look\e[m\n\n"
# foreground colors
for fgc in {30..37}; do
# background colors
for bgc in {40..47}; do
fgc=${fgc#37} # white
bgc=${bgc#40} # black
vals="${fgc:+$fgc;}${bgc}"
vals=${vals%%;}
seq0="${vals:+\e[${vals}m}"
printf " %-9s" "${seq0:-(default)}"
printf " ${seq0}TEXT\e[m"
printf " \e[${vals:+${vals+$vals;}}1mBOLD\e[m"
done
echo; echo
done
}
errata
does 1 mean bold or light? i have confusing terminology here.