ANSI Color

TL;DR

A nice shell script to showcase coloring the output.

The Inconsolation series of posts is excellent for wasting a lot of your time, if you want to know. But who knows? One of these gems might come handy one day or another.

I can’t remember how to find the exact post there, but I found an interesting script to colorize the output in the shell… find it here: ANSI colorschemes scripts.

The script there suffers from pasting the contents inside a web page, in that the value of variable esc inside function initializeANSI is lost. I took the liberty of putting an alternative way to populate it with the right value, in a way that’s POSIX compliant and also easily copy-pasteable. You can find an easily transferable copy here (yes, copy and paste works too with this script):

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#!/bin/sh

# ANSI Color -- use these variables to easily have different color
#    and format output. Make sure to output the reset sequence after 
#    colors (f = foreground, b = background), and use the 'off'
#    feature for anything you turn on.
#
# Adapted from https://crunchbang.org/forums/viewtopic.php?id=13645
# I took the liberty to set the value for variable esc so that it is
# preserved through copy-paste in websites.

initializeANSI()
{
  esc="$(printf %b \\033)"

  blackf="${esc}[30m";   redf="${esc}[31m";    greenf="${esc}[32m"
  yellowf="${esc}[33m"   bluef="${esc}[34m";   purplef="${esc}[35m"
  cyanf="${esc}[36m";    whitef="${esc}[37m"
  
  blackb="${esc}[40m";   redb="${esc}[41m";    greenb="${esc}[42m"
  yellowb="${esc}[43m"   blueb="${esc}[44m";   purpleb="${esc}[45m"
  cyanb="${esc}[46m";    whiteb="${esc}[47m"

  boldon="${esc}[1m";    boldoff="${esc}[22m"
  italicson="${esc}[3m"; italicsoff="${esc}[23m"
  ulon="${esc}[4m";      uloff="${esc}[24m"
  invon="${esc}[7m";     invoff="${esc}[27m"

  reset="${esc}[0m"
}

# note in this first use that switching colors doesn't require a reset
# first - the new color overrides the old one.

clear 

initializeANSI

cat << EOF

 ${yellowf}  β–„β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–„${reset}   ${redf}  β–„β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–„${reset}    ${greenf}  β–„β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–„${reset}    ${bluef}  β–„β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–„${reset}    ${purplef}  β–„β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–„${reset}    ${cyanf}  β–„β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–„${reset}
 ${yellowf}β–„β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–€β–€${reset}  ${redf}β–„${whitef}β–ˆβ–€β–ˆ${redf}β–ˆβ–ˆ${whitef}β–ˆβ–€β–ˆ${redf}β–ˆβ–ˆβ–„${reset}  ${greenf}β–„${whitef}β–ˆβ–€β–ˆ${greenf}β–ˆβ–ˆ${whitef}β–ˆβ–€β–ˆ${greenf}β–ˆβ–ˆβ–„${reset}  ${bluef}β–„${whitef}β–ˆβ–€β–ˆ${bluef}β–ˆβ–ˆ${whitef}β–ˆβ–€β–ˆ${bluef}β–ˆβ–ˆβ–„${reset}  ${purplef}β–„${whitef}β–ˆβ–€β–ˆ${purplef}β–ˆβ–ˆ${whitef}β–ˆβ–€β–ˆ${purplef}β–ˆβ–ˆβ–„${reset}  ${cyanf}β–„${whitef}β–ˆβ–€β–ˆ${cyanf}β–ˆβ–ˆ${whitef}β–ˆβ–€β–ˆ${cyanf}β–ˆβ–ˆβ–„${reset}
 ${yellowf}β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–€${reset}      ${redf}β–ˆ${whitef}β–„β–„β–ˆ${redf}β–ˆβ–ˆ${whitef}β–„β–„β–ˆ${redf}β–ˆβ–ˆβ–ˆ${reset}  ${greenf}β–ˆ${whitef}β–„β–„β–ˆ${greenf}β–ˆβ–ˆ${whitef}β–„β–„β–ˆ${greenf}β–ˆβ–ˆβ–ˆ${reset}  ${bluef}β–ˆ${whitef}β–„β–„β–ˆ${bluef}β–ˆβ–ˆ${whitef}β–„β–„β–ˆ${bluef}β–ˆβ–ˆβ–ˆ${reset}  ${purplef}β–ˆ${whitef}β–„β–„β–ˆ${purplef}β–ˆβ–ˆ${whitef}β–„β–„β–ˆ${purplef}β–ˆβ–ˆβ–ˆ${reset}  ${cyanf}β–ˆ${whitef}β–„β–„β–ˆ${cyanf}β–ˆβ–ˆ${whitef}β–„β–„β–ˆ${cyanf}β–ˆβ–ˆβ–ˆ${reset}
 ${yellowf}β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–„${reset}      ${redf}β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ${reset}  ${greenf}β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ${reset}  ${bluef}β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ${reset}  ${purplef}β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ${reset}  ${cyanf}β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ${reset}
 ${yellowf}β–€β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–„β–„${reset}  ${redf}β–ˆβ–ˆβ–€β–ˆβ–ˆβ–€β–€β–ˆβ–ˆβ–€β–ˆβ–ˆ${reset}  ${greenf}β–ˆβ–ˆβ–€β–ˆβ–ˆβ–€β–€β–ˆβ–ˆβ–€β–ˆβ–ˆ${reset}  ${bluef}β–ˆβ–ˆβ–€β–ˆβ–ˆβ–€β–€β–ˆβ–ˆβ–€β–ˆβ–ˆ${reset}  ${purplef}β–ˆβ–ˆβ–€β–ˆβ–ˆβ–€β–€β–ˆβ–ˆβ–€β–ˆβ–ˆ${reset}  ${cyanf}β–ˆβ–ˆβ–€β–ˆβ–ˆβ–€β–€β–ˆβ–ˆβ–€β–ˆβ–ˆ${reset}
 ${yellowf}  β–€β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–€${reset}   ${redf}β–€   β–€  β–€   β–€${reset}  ${greenf}β–€   β–€  β–€   β–€${reset}  ${bluef}β–€   β–€  β–€   β–€${reset}  ${purplef}β–€   β–€  β–€   β–€${reset}  ${cyanf}β–€   β–€  β–€   β–€${reset}
 
 ${boldon}${yellowf}  β–„β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–„   ${redf}  β–„β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–„    ${greenf}  β–„β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–„    ${bluef}  β–„β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–„    ${purplef}  β–„β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–„    ${cyanf}  β–„β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–„${reset}
 ${boldon}${yellowf}β–„β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–€β–€  ${redf}β–„${whitef}β–ˆβ–€β–ˆ${redf}β–ˆβ–ˆ${whitef}β–ˆβ–€β–ˆ${redf}β–ˆβ–ˆβ–„  ${greenf}β–„${whitef}β–ˆβ–€β–ˆ${greenf}β–ˆβ–ˆ${whitef}β–ˆβ–€β–ˆ${greenf}β–ˆβ–ˆβ–„  ${bluef}β–„${whitef}β–ˆβ–€β–ˆ${bluef}β–ˆβ–ˆ${whitef}β–ˆβ–€β–ˆ${bluef}β–ˆβ–ˆβ–„  ${purplef}β–„${whitef}β–ˆβ–€β–ˆ${purplef}β–ˆβ–ˆ${whitef}β–ˆβ–€β–ˆ${purplef}β–ˆβ–ˆβ–„  ${cyanf}β–„${whitef}β–ˆβ–€β–ˆ${cyanf}β–ˆβ–ˆ${whitef}β–ˆβ–€β–ˆ${cyanf}β–ˆβ–ˆβ–„${reset}
 ${boldon}${yellowf}β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–€      ${redf}β–ˆ${whitef}β–„β–„β–ˆ${redf}β–ˆβ–ˆ${whitef}β–„β–„β–ˆ${redf}β–ˆβ–ˆβ–ˆ  ${greenf}β–ˆ${whitef}β–„β–„β–ˆ${greenf}β–ˆβ–ˆ${whitef}β–„β–„β–ˆ${greenf}β–ˆβ–ˆβ–ˆ  ${bluef}β–ˆ${whitef}β–„β–„β–ˆ${bluef}β–ˆβ–ˆ${whitef}β–„β–„β–ˆ${bluef}β–ˆβ–ˆβ–ˆ  ${purplef}β–ˆ${whitef}β–„β–„β–ˆ${purplef}β–ˆβ–ˆ${whitef}β–„β–„β–ˆ${purplef}β–ˆβ–ˆβ–ˆ  ${cyanf}β–ˆ${whitef}β–„β–„β–ˆ${cyanf}β–ˆβ–ˆ${whitef}β–„β–„β–ˆ${cyanf}β–ˆβ–ˆβ–ˆ${reset}
 ${boldon}${yellowf}β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–„      ${redf}β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ  ${greenf}β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ  ${bluef}β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ  ${purplef}β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ  ${cyanf}β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ${reset}
 ${boldon}${yellowf}β–€β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–„β–„  ${redf}β–ˆβ–ˆβ–€β–ˆβ–ˆβ–€β–€β–ˆβ–ˆβ–€β–ˆβ–ˆ  ${greenf}β–ˆβ–ˆβ–€β–ˆβ–ˆβ–€β–€β–ˆβ–ˆβ–€β–ˆβ–ˆ  ${bluef}β–ˆβ–ˆβ–€β–ˆβ–ˆβ–€β–€β–ˆβ–ˆβ–€β–ˆβ–ˆ  ${purplef}β–ˆβ–ˆβ–€β–ˆβ–ˆβ–€β–€β–ˆβ–ˆβ–€β–ˆβ–ˆ  ${cyanf}β–ˆβ–ˆβ–€β–ˆβ–ˆβ–€β–€β–ˆβ–ˆβ–€β–ˆβ–ˆ${reset}
 ${boldon}${yellowf}  β–€β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–€   ${redf}β–€   β–€  β–€   β–€  ${greenf}β–€   β–€  β–€   β–€  ${bluef}β–€   β–€  β–€   β–€  ${purplef}β–€   β–€  β–€   β–€  ${cyanf}β–€   β–€  β–€   β–€${reset}

EOF

Local version here.

The trick is all in line 14: thanks to the excellent tips in Rich’s sh (POSIX shell) tricks (covered in the same-named post here), we are able to avoid putting the binary value for the escape character and populate environment variable env with the right value by only using printable characters. Yay!

If you trust yours truly (and GitLab, for what I know) you can just run it from the command line:

u='https://gitlab.com/polettix/notechs/-/snippets/2039857/raw/master/ansi-color.sh'
curl -L "$u" | sh

Demo time:

I think it works even better in my terminal:

ansi-color output

Life needs some color… and remember to stay safe please!


Comments? Octodon, , GitHub, Reddit, or drop me a line!