121

Throughout the internet I have seen many people with scripts that prints out a bunch of stuff in all the colors defined in ~/.Xdefaults. However when I try to use these, I always get error: Bad Substitution. Does anyone have a working script that does the same thing?

It should end up looking something like this: enter image description here

Isaiah
  • 58,486
  • 28
  • 133
  • 145
John Howard
  • 1,945
  • 3
  • 14
  • 8
  • 1
    Maybe it's easier if you post the script you're using, and how and from where you're invocating it. – luri Feb 20 '11 at 21:56
  • With one of the scripts that don't work, try to change the first line to `#!/bin/sh` (if it isn't already that) and see if it makes a difference (it should be something similar) – Stefano Palazzo Feb 20 '11 at 22:07
  • A useful function along these lines for emacs users: `M-x list-colors-display`. – Brian Z Mar 24 '15 at 08:17
  • 1
    You can find a collection of script for this in the [ArchWiki](https://wiki.archlinux.org/index.php/x_resources#Color_scheme_scripts). – aloisdg Jan 08 '16 at 07:44
  • 1
    I packaged a few color printing scripts, and put them [here](https://github.com/pvinis/colortools), complete with examples, thanks, and brew installation. – pvinis Jul 25 '16 at 08:15
  • 1
    @pvinis thanks! I spent way too much time trying to find the exact one shown in the screen of OP. – verboze Nov 30 '18 at 23:14
  • 1
    FWIW, this seems to be where the image came from: https://web.archive.org/web/20110809032233/https://bbs.archlinux.org/viewtopic.php?id=51818&p=2#p652393. The post includes the colour scheme used, and the opening post in the thread cites the source code used to generate the picture. – mwfearnley Dec 21 '18 at 14:49

12 Answers12

135

Here is my solution with Bash only:

for x in {0..8}; do 
    for i in {30..37}; do 
        for a in {40..47}; do 
            echo -ne "\e[$x;$i;$a""m\\\e[$x;$i;$a""m\e[0;37;40m "
        done
        echo
    done
done
echo ""

One-liner:

for x in {0..8}; do for i in {30..37}; do for a in {40..47}; do echo -ne "\e[$x;$i;$a""m\\\e[$x;$i;$a""m\e[0;37;40m "; done; echo; done; done; echo ""

Here's a picture in Cygwin:

Cygwin screenshot

wjandrea
  • 14,109
  • 4
  • 48
  • 98
oktay
  • 1,504
  • 1
  • 10
  • 3
  • 2
    FYI, `{30..37}` is equivalent to `\`seq 30 37\`` in bash and is faster. – Barry Kelly Dec 16 '15 at 05:18
  • 2
    @BarryKelly: good tip, thanks. just tested and as you noted: it's really much faster!!! :-) so here is the new one: `for x in 0 1 4 5 7 8; do for i in {30..37}; do for a in {40..47}; do echo -ne "\e[$x;$i;$a""m\\\e[$x;$i;$a""m\e[0;37;40m "; done; echo; done; done; echo ""; ` – oktay Dec 17 '15 at 11:56
  • Will this work on a Mac ? – code-8 Feb 10 '17 at 15:13
  • @ihue See if this answers your question: [Simple Tricks to Improve the Terminal Appearance in Mac OS X](http://osxdaily.com/2013/02/05/improve-terminal-appearance-mac-os-x/). For more details, check http://AskDifferent.com – wjandrea Feb 12 '17 at 00:56
  • Sample code did not work on MacOS but the python answer worked. – sorin Jul 03 '17 at 09:42
  • 1
    This also works on macOS, just replace `\e` with `\033`. – Atika Dec 06 '19 at 08:59
97

A simple one-liner that should work for most people.

msgcat --color=test
Jacek Trociński
  • 1,079
  • 7
  • 3
  • 1
    Deadsimple. Is it possible to get only default background color? – vrcmr Aug 03 '18 at 21:18
  • 1
    msgcat --help doesn't explain waht you did... Is it an easter egg? – vrcmr Aug 03 '18 at 21:30
  • @vrcmr I stumbled across this one-liner in a blog somewhere, sometime ago and unfortunately could not find any mention of `--color=test` in the msgcat man page as well. I consider it to be a non-configurable easter egg. – Jacek Trociński Aug 06 '18 at 12:00
  • 2
    For anyone else interested, `--color=test` is mentioned in section 9.11.2 The environment variable 'TERM' under `info msgcat` – Gerrit0 Sep 05 '19 at 02:44
67

You can also use the colortest Install colortest package.

  1. Install it with this command:

    sudo apt-get install colortest
    
  2. It provides several commands which you can use, depending on how many colors you want:

    colortest-16   colortest-16b  colortest-256  colortest-8
    

Example output from colortest-16b:

enter image description here

kiri
  • 27,676
  • 16
  • 81
  • 117
36

Here's my version:

#!/usr/bin/env python
import sys
terse = "-t" in sys.argv[1:] or "--terse" in sys.argv[1:]
write = sys.stdout.write
for i in range(2 if terse else 10):
    for j in range(30, 38):
        for k in range(40, 48):
            if terse:
                write("\33[%d;%d;%dm%d;%d;%d\33[m " % (i, j, k, i, j, k))
            else:
                write("%d;%d;%d: \33[%d;%d;%dm Hello, World! \33[m \n" %
                      (i, j, k, i, j, k,))
        write("\n")

This prints everything. If you want a nice table (that only shows style (0) and (1), normal and bold), you can use the -t or --terse argument:

The 'blink' style (5) doesn't work with gnome-terminal. ;-)


If this doesn't work for you, there's something else wrong. Please let us know once you've tested it.

Rufflewind
  • 621
  • 5
  • 6
Stefano Palazzo
  • 85,787
  • 45
  • 210
  • 227
  • My prompt color is defined like this: `DULL=0 BRIGHT=1 FG_WHITE=37 WHITE="\[$ESC[${DULL};${FG_WHITE}m\]"` Why is it, that the dull white (`0;37;40`) is more gray than white, and the bright white (`1;37;40`) is bolded? I'd like to set the terminal font to bright-white-on-black, not bolded. When I run your script, `1;37;40` looks perfectly allright _after the first line_: http://5img.com/img13/740/24screenshot.png So, my PS1 line is in the color of the text `0;30;40`; I'd like it in the color of `0;30;41`. – appas Nov 17 '11 at 10:59
  • Does anyone else find this magenta particularly ugly? It looks like mud. – Ether Jun 13 '12 at 18:37
  • You're a god among men. Best cross-platform solution. Thanks – airstrike Feb 13 '19 at 19:47
11

i made a little script for that :)

here's the important part:

colors=$@
for (( n=0; n < $colors; n++ )) do
    printf " [%d] $(tput setaf $n)%s$(tput sgr0)" $n "wMwMwMwMwMwMw
"
done

you pass it a number n and it spits out n colored lines along with each color's ansi index (you can use it in $(tput setaf <ansi-index>)).

here's a screenshot of the (partial) output:

ansi-colors

i also got this one, which i forked (and slightly modified) from twerth:

#!/usr/bin/env bash

echo -e "\033[0mNC (No color)"
echo -e "\033[1;37mWHITE\t\033[0;30mBLACK"
echo -e "\033[0;34mBLUE\t\033[1;34mLIGHT_BLUE"
echo -e "\033[0;32mGREEN\t\033[1;32mLIGHT_GREEN"
echo -e "\033[0;36mCYAN\t\033[1;36mLIGHT_CYAN"
echo -e "\033[0;31mRED\t\033[1;31mLIGHT_RED"
echo -e "\033[0;35mPURPLE\t\033[1;35mLIGHT_PURPLE"
echo -e "\033[0;33mYELLOW\t\033[1;33mLIGHT_YELLOW"
echo -e "\033[1;30mGRAY\t\033[0;37mLIGHT_GRAY"

… which, in my current theme, shows:

terminal puts out (it's colors)!

Eliran Malka
  • 1,205
  • 18
  • 36
  • Will this work on the Mac ? – code-8 Feb 10 '17 at 15:12
  • Is these the only 7 colors we can have ??? – code-8 Feb 10 '17 at 15:12
  • @ihue - of course not, i made a script for that.. [check it out](https://gist.github.com/eliranmal/b373abbe1c21e991b394bdffb0c8a6cf). you can pass it a number `n` (e.g. `256`) and it will spit out the `n` colors your terminal supports. – Eliran Malka Feb 11 '17 at 23:26
  • 1
    Your script is cool, but `#!/usr/bin/env sh` is not a good idea. It doesn't work with the default shell of Ubuntu, which is dash. I had to change it to bash. – admirabilis Nov 08 '17 at 04:03
  • 1
    Awesome script! – desgua Jan 09 '21 at 17:50
  • please set `colors=256` in your first example. minimal version: `colors=256; for (( n=0;n – milahu Apr 07 '22 at 10:11
  • @MilaNautikus, thanx for the suggestion, but that deviates from the original intent of this post. you can post your own answer with that snippet :) – Eliran Malka Apr 21 '22 at 19:40
8

Recently wanted to find that script that many people are refering myself. It's from the tldp.org Bash Prompt HOWTO - http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html. The script is authored by Daniel Crisman.

It outputs exactly the same as on the pic from the question. The script itself:

#!/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
Illia
  • 81
  • 1
  • 1
5

This is my solution. It prints all 225 colours in bash:

for colour in {1..225}
    do echo -en "\033[38;5;${colour}m38;5;${colour} \n"
done | column -x
Jake Ireland
  • 153
  • 1
  • 4
  • This is a nice addition to the other answers here. There are *more* colors here than other answers. – ephsmith May 03 '22 at 17:27
4

This question is actually a top result when I search on how to display color codes in a terminal. So I wanted to give justice and give what the OP exactly was looking for. I do remember the screenshot is somewhat familiar. At first, I thought it was from Gogh however it's a bit different. I then realized it's exactly the same script being used in iTerm2 colors.

Lucky enough, they added a comment on where it originally came from

I'm posting the script for reference, taken from iTerm2 with original credits:

#!/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).
#
#   Copied from http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html

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

Here's the script in action:

enter image description here

chriz
  • 169
  • 2
1

Refer https://askubuntu.com/a/396555/41013 That will print the following output with formats like BOLD ,UNDERLINE , Highlighting and colors.

Small script to display possible terminal colors

1

This is a modified version of the TLDP script here. It shows standard colors and vivid colors (codes 90-97 and 100-107).

#!/bin/bash
# Show available terminal colours.
# Heavily modified version of the TLDP script here:
# http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html

print_colors(){
  # Print column headers.
  printf "%-4s  " '' ${bgs[@]}
  echo
  # Print rows.
  for bold in ${bolds[@]}; do
    for fg in ${fgs[@]}; do
      # Print row header
      printf "%s;%s  " $bold $fg
      # Print cells.
      for bg in ${bgs[@]}; do
        # Print cell.
        printf "\e[%s;%s;%sm%s\e[0m  " $bold $fg $bg "text"
      done
      echo
    done
  done
}

# Print standard colors.
bolds=( 0 1 )
fgs=( 3{0..7} )
bgs=( 4{0..8} )
print_colors

# Print vivid colors.
bolds=( 0 ) # Bold vivid is the same as bold normal.
fgs=( 9{0..7} )
bgs=( 10{0..8} )
print_colors

Example output:

gnome-terminal screenshot

wjandrea
  • 14,109
  • 4
  • 48
  • 98
0

I wrote a little script that prints all the colors with number value, rgb value,hex value ,... so that I never have to look for the 256 terminal colors on the internet. You can find the link here. You can also get it from pypi:

pip3 install colo

0

Since we are talking old school, a good old ANSI C program is in order:

#include <stdio.h>

static void aput( char* c, char bg_col, char fg_col )
{  int bg, fg;
   if( bg_col > 7 ) bg = 100 + bg_col-8;
   else             bg =  40 + bg_col;
   if( fg_col > 7 ) fg =  90 + fg_col-8;
   else             fg =  30 + fg_col;
   printf( "\033[%i;%im%s\033[0;0m", fg, bg, c );
}

int main( void )
{  int bg, fg;
   
   for( fg = 0; fg <  8; fg += 1 ) aput("    ", fg, 0); putchar('\n');
   for( fg = 8; fg < 16; fg += 1 ) aput("    ", fg, 0); putchar('\n');
   putchar('\n'); 
   for( bg = 0; bg < 16; bg += 1 )
   {  for( fg = 0; fg < 16; fg += 1 )
      {   aput( "10", bg, fg );  }
      putchar('\n');
   }
}

Sample output:

enter image description here