How to create a testcolor.sh like the following screenshot?

install the package named as:

colortest

and enjoy coloring by running the binaries like colortest-8 colortest-16 colortest-16b and so on


The escape sequence ESC [ SPEC1 ; … m changes the text attributes (color, bold, etc.) of subsequently written characters. This is one of the ANSI terminal escape sequences. Each SPEC can be one of the following (the list is not exhaustive):

  • 0 to switch to the default colors.
  • 1 to turn boldface on.
  • 30 through 37 to set the foreground color (black, red, green, yellow, blue, magenta, cyan, gray).
  • 40 through 47 to set the background color (same list).

Here's a shell snippet that outputs almost what you posted.

printf "          "
for b in 0 1 2 3 4 5 6 7; do printf "  4${b}m "; done
echo
for f in "" 30 31 32 33 34 35 36 37; do
    for s in "" "1;"; do
        printf "%4sm" "${s}${f}"
        printf " \033[%sm%s\033[0m" "$s$f" "gYw "
        for b in 0 1 2 3 4 5 6 7; do
            printf " \033[4%s;%sm%s\033[0m" "$b" "$s$f" " gYw "
        done
        echo
     done
done

While it's not that hard to replicate, your screenshot likely came from tldp.org; the bottom of that page contains a script that outputs the table you see:

To help myself remember what colours are available, I wrote a script that output all the colours to the screen. Daniel Crisman has supplied a much nicer version which I include below:

#!/bin/bash
#
#   This file echoes a bunch of color codes to the 
#   terminal to demonstrate what's available.  Each 
#   line is the color code of one forground color,
#   out of 17 (default + 16 escapes), followed by a 
#   test use of that color on all nine background 
#   colors (default + 8 escapes).
#

T='gYw'   # The test text

echo -e "\n                 40m     41m     42m     43m\
     44m     45m     46m     47m";

for FGs in '    m' '   1m' '  30m' '1;30m' '  31m' '1;31m' '  32m' \
           '1;32m' '  33m' '1;33m' '  34m' '1;34m' '  35m' '1;35m' \
           '  36m' '1;36m' '  37m' '1;37m';
  do FG=${FGs// /}
  echo -en " $FGs \033[$FG  $T  "
  for BG in 40m 41m 42m 43m 44m 45m 46m 47m;
    do echo -en "$EINS \033[$FG\033[$BG  $T  \033[0m";
  done
  echo;
done
echo

There's a similar table available as a demo in the F-ANSI library:

ANSI color table generated by F-ANSI