colors in bash — script to display all the possible colors.

If you wanted to have colors in the bash output (including the colors in PS1-4), don\’t you keep wondering how the color code would look on the terminal. So, I wrote this small script to show the complete color codes. This is how the output would look:

\"Bash

And here is the 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
#!/bin/bash -
#===============================================================================
#
#          FILE:  colors.sh
#
#         USAGE:  ./colors.sh
#
#   DESCRIPTION:  Bash colors
#
#       OPTIONS:  ---
#  REQUIREMENTS:  ---
#          BUGS:  ---
#         NOTES:  ---
#        AUTHOR:  Amit Agarwal (AKA), [email protected]
#       COMPANY:  Individual
#       VERSION:  1.0
#       CREATED:  09/21/2009 06:12:07 PM IST
#      REVISION:  ---
#===============================================================================

for c in `seq 0 255`;
do
t=5;
[[ $c -lt 108 ]]&&t=0;
for i in `seq $t 5`;
do
#Display the codes also for easier lookup in terminal
<!--loginview start-->echo $i;${c}<!--loginview end-->
echo -e \"<a class="zem_slink" title="Path (computing)" rel="wikipedia" href="http://en.wikipedia.org/wiki/Path_%28computing%29">\\</a>\e[0;48;$i;${c}m|| $i:$c `seq -s+0 $(($COLUMNS/2))|tr -d \'[0-9]\'`\\e[0m\";
done;
done

# setup_colors - Adds colors to array CC for global use
# 30 - Black, 31 - Red, 32 - Green, 33 - Yellow, 34 - Blue,
# 35 - Magenta, 36 - Blue/Green, 37 - White,
# 30/42 - Black on Green \'30\\;42\'
<!--loginview start-->function setup_colors(){
declare -a CC;
for i in `seq 0 7`;
do
ii=$(($i+7));
CC[$i]=\"\\033[1;3${i}m\";
CC[$ii]=\"\\033[0;3${i}m\";
done;
CC[15]=\"\\033[30;42m\";
R=$\'\\033[0;00m\';
X=$\'\\033[1;37m\';
export R X;
}
function display_colors(){
for i in $(seq 0 $((${#CC[@]} - 1))); do echo -e \"${CC[$i]}[$i]\\n$R\"; done
}
\"Reblog
Enhanced by Zemanta

About Amit Agarwal

4 Comments on “colors in bash — script to display all the possible colors.”

  1. Finn

    Just trying out your interesting looking script, but running into some problems.

    First the code in the post includes non-standard ” and ‘ characters. I’ve fixed that problem and posted it to pastebin: http://pastebin.com/DUu2e3bM

    The formating isn’t working on my system (Mint 10) for some reason. Each time the loop runs it emits a color block and the following error:

    1
    2
    colors: line 27: 255: command not found
    colors: line 28: /2: syntax error: operand expected (error token is "/2")

    Any ideas what the problem might be?

    Thanks!

  2. Jay

    Is this to be used in bash? There are a slew of incorrect lines if so. Slackware 13.1

    line 24: syntax error near unexpected token `;&’
    line 24: `[[ $c -lt 108 ]]&&t=0;’
    line: 28: echo $i;${c}
    line 28: echo $i;${c}
    line 29:

1 Trackback on “colors in bash — script to display all the possible colors.”