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):
#!/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 initializeANSIcat<<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
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:
Life needs some color⦠and remember to stay safe please!