Re:Production is Red, Development is Blue
by xtacy on Monday January 10, 2011 @03:08PM
(#2089561)
To avoid cryptic looking escape sequences, one could have the colour codes named in their .?shrc file [1]:
[1] https://wiki.archlinux.org/index.php/Color_Bash_Prompt
[ Parent
| on HN ]
I have a similar section in my .bashrc via a somewhat hackish loop (or two):
ESC=$'\x1b'
# Terminal color escapes
colornames=(BLACK RED GREEN YELLOW BLUE MAGENTA CYAN)
for i in ${!colornames[@]}; do
eval "${colornames[$i]}=\"${ESC}[3${i}m\""
eval "BG${colornames[$i]}=\"${ESC}[4${i}m\""
done
# There are holes in this sequence, so it doesn't loop nicely
BOLD="${ESC}[1m"
DARK="${ESC}[2m"
UNDERLINE="${ESC}[4m"
BLINK="${ESC}[5m"
INVERSE="${ESC}[7m"
RST="${ESC}[m"
styles=(BOLD DARK UNDERLINE BLINK INVERSE RST)
# and the PS1-safe versions of the above
for i in ${colornames[@]} ${colornames[@]/#/BG} ${styles[@]}; do
eval "PS$i=\"\\[\$$i\\]\""
done
Having color escapes in shell variables comes in handy from time to time (like faking 'colordiff' with sed).