How can I output colored text using \"printf\" on both Mac OS X and Linux?
For the best portability, query the terminfo database. In shell,
colors=(black red green yellow blue magenta cyan white)
for ((i = 0; i < ${#colors[*]}; i++)); do
((j=(i+1)%${#colors[*]}))
printf '%s%s%s on %s%s\n' "$(tput setaf $i)" "$(tput setab $j)" \
"${colors[i]}" "${colors[j]}" "$(tput op)"
done
will print out
black on red red on green green on yellow yellow on blue blue on magenta magenta on cyan cyan on white white on black
but in color.