Skip to main content
5 of 7
added 13 characters in body

ANSI Colors on console term

Preamble: about $TERM variable

Verify your environment with: tput colors

If you use some compatible xterm console, your $TERM may contain something like xterm*:

echo $TERM xterm tput colors 8 

Further demos won't work: you will get image looking like:

enter image description here

unless you set this to xterm-256color:

export TERM=xterm-256color ; reset tput colors 256 

( You could even try to run this after setting export TERM=xterm-mono ;)

Doing simply the job by using tput

Depending on wich term protocol your console use, sequence could be: \e[38;5;XXXm or \e[3XXXm where XXX correspond to ansi number.

To ensure you use the right ANSI sequence, you have to use tput.

Regarding to Wikipedia's ANSI escape code, I wrote this:

#!/bin/bash for ((i=0; i<256; i++)) ;do echo -n ' ' tput setab $i tput setaf $(( ( (i>231&&i<244 ) || ( (i<17)&& (i%8<2)) || (i>16&&i<232)&& ((i-16)%6 <(i<100?3:2) ) && ((i-16)%36<15) )?7:16)) printf " C %03d " $i tput op (( ((i<16||i>231) && ((i+1)%8==0)) || ((i>16&&i<232)&& ((i-15)%6==0)) )) && printf "\n" '' done 

Could render something like:

enter image description here

Optimizing : reducing forks by running tput as background process

... Then, because I hate runnig more than 200 forks in a little script, I wrote this:

#!/bin/bash # Connector fifos directory read TMPDIR < <(mktemp -d /dev/shm/conn_shell_XXXXXXX) fd=3 # find next free fd nextFd() { while [ -e /dev/fd/$fd ];do ((fd++)) ;done;printf -v $1 %d $fd;} tputConnector() { mkfifo $TMPDIR/tput nextFd TPUTIN eval "exec $TPUTIN> >(LANG=C exec stdbuf -o0 tput -S - >$TMPDIR/tput 2>&1)" nextFd TPUTOUT eval "exec $TPUTOUT<$TMPDIR/tput" rm $TMPDIR/tput rmdir $TMPDIR } myTput() { echo -e "$1\ncr" 1>&$TPUTIN && IFS= read -r -d $'\r' -u $TPUTOUT $2 } tputConnector myTput op op myTput "setaf 7" grey myTput "setaf 16" black fore=("$black" "$grey") for ((i=0; i<256; i++)) ;do myTput "setab $i" bgr printf " %s%s %3d %s" "$bgr" "${fore[ i>231 && i<244||(i<17)&& (i%8<2)|| (i>16&&i<232)&&((i-16)%6*11+(i-16)/6%6*14+(i-16)/36*10)<58 ? 1 : 0 ]}" $i "$op" (( ((i<16||i>231) && ((i+1)%8==0)) || ((i>16&&i<232)&& ((i-15)%6==0)) )) && printf "\n" '' done 

With only 1 fork! Same result, but approx 10x faster!

If first script run in ~1.12 second on my desk, they take upto ~47.4 seconds on my raspberry-pi!!

While second script run in ~0.11 second on my desk and ~4.97 seconds on my raspberry.