Wednesday, March 26, 2008

Using Color In The Linux Or Unix Shell

ECMA-compliant Color Chart Via The Shell

Click the image above to see all the pretty colors bigger ;)

Hey There,

Today's we're going to go off on a tangent and take a look at color and the Linux and/or Unix console. I first noticed the use of color in the shell while using Linux a long long time ago, and it seemed really cool back then. Every now and again it bugs me, but I must admit that it does come in handy for a lot of things; editing code or simple file type identification are two great examples.

The script attached to this post was written mostly to illustrate the concept that you can see in the picture attached to this text. More to the point, I wrote it so that we could see the colors I'm writing about :) Admittedly, I used every possible combination of color and modification for completeness, which has the nasty side effect of making some of the output invisible (You can't see the black foreground on the black background unless reverse video or bolding are turned on), but that's the nature of the beast. You probably won't ever need or want to print black on black, unless you're writing a menuing system for someone you can't stand ;)

One thing to note is that the colors and modifications in this script are limited to the ECMA-compliant codes. Linux and the Bash shell, for example, have a lot more color options, but I wanted to keep this post as fairly balanced as possible. These codes should work on almost any Unix or Linux console and most SSH or Telnet shell clients today probably support them as well. Still, not everything will work everywhere. For instance, the blinking text code does not work on my PuTTY terminal, although it does work on my SecureCRT. Since I'm not paying for the color, I guess I can't complain ;)

Getting these colors to show on your terminal is actually fairly simple. First, you just need to know what all the codes stand for. Below is a quick listing. The 30's are foregrounds, the 40's are backgrounds and the remaining set are modifications (any and all of which can be combined, if you so choose):

# 30 black foreground
# 31 red foreground
# 32 green foreground
# 33 brown foreground
# 34 blue foreground
# 35 purple foreground
# 36 light blue foreground
# 37 gray foreground
#
# 40 black background
# 41 red background
# 42 green background
# 43 brown background
# 44 blue background
# 45 purple background
# 46 light blue background
# 47 white background
#
# 1 turn on bolding
# 22 turn off bolding
# 5 turn on blinking
# 25 turn off blinking
# 7 turn on reverse video
# 27 turn off reverse video
# 0 reset everything to default


Now, if you wanted to print a color to your screen, all you need to do is use the echo command (Note that -e tells echo to interpret the backslashed characters, so "\033[30m" doesn't show up literally). For instance, to print "HI" in red font, you could type:

host # echo -e "\033[31mHI"

The "m" character following the number code 31 (Indicating that you want to print in red) completes the instantiation of the color. Without it, your text would remain whatever color it already was. The incorrect echo statement may also have other bizarre side effects, like repositioning your cursor or resetting your terminal. The final "m" is very important :)

You can also combine different colors, as well as modifications, by using the semicolon. Below, the first line writes "HI" in light blue on a green background (Uggh) and the second does the same in BOLD :)

host # echo -e "\033[36;42mHI"
host # echo -e "\033[36;42;1mHI"


One thing to note is that all of these commands would leave your terminal stuck in whatever state you instructed on the command line. That is, if you used the color commands to type with a green background, your terminal's background would remain green after you typed the line. In order to return everything to normal, you can use the modification number "0" - This will reset your terminal to the default color settings. If you only want to print one line in red font, the line shown below would work better for you than the original (the first example, above)

host # echo -e "\033[31mHI\033[0m"

Note, also, that (for the color code commands) there can't be any spaces. "\033[31m" has to be one unit. The text you put in between this command and the closing "\033[0m" (should you elect to go back to the way things were ;) can have spaces anywhere, just like any other block of text.

Hope you have some fun with shell colors. Enjoy the script. If nothing else, it should serve as a good reminder of the syntax and a good indicator of the breadth of options ECMA compliant colors give you. Linux, as I mentioned, has even more variety, but using those specific codes can make your scripts less portable. Good or bad? You decide ;)

Cheers,


Creative Commons License


This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License

#!/bin/bash

# colors.sh - Print out all the different ECMA color-mod combos
#
# 2008 - Mike Golvach - eggi@comcast.net
#
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#

declare -a color_array
declare -a alphabet_soup

color_array=( '30;40;1' '30;40;5' '30;40;7' '30;40;22' '30;40;25' '30;40;27' '30;40' '30;41;1' '30;41;5' '30;41;7' '30;41;22' '30;41;25' '30;41;27' '30;41' '30;42;1' '30;42;5' '30;42;7' '30;42;22' '30;42;25' '30;42;27' '30;42' '30;43;1' '30;43;5' '30;43;7' '30;43;22' '30;43;25' '30;43;27' '30;43' '30;44;1' '30;44;5' '30;44;7' '30;44;22' '30;44;25' '30;44;27' '30;44' '30;45;1' '30;45;5' '30;45;7' '30;45;22' '30;45;25' '30;45;27' '30;45' '30;46;1' '30;46;5' '30;46;7' '30;46;22' '30;46;25' '30;46;27' '30;46' '30;47;1' '30;47;5' '30;47;7' '30;47;22' '30;47;25' '30;47;27' '30;47' '30;1' '30;5' '30;7' '30;22' '30;25' '30;27' '30' '31;40;1' '31;40;5' '31;40;7' '31;40;22' '31;40;25' '31;40;27' '31;40' '31;41;1' '31;41;5' '31;41;7' '31;41;22' '31;41;25' '31;41;27' '31;41' '31;42;1' '31;42;5' '31;42;7' '31;42;22' '31;42;25' '31;42;27' '31;42' '31;43;1' '31;43;5' '31;43;7' '31;43;22' '31;43;25' '31;43;27' '31;43' '31;44;1' '31;44;5' '31;44;7' '31;44;22' '31;44;25' '31;44;27' '31;44' '31;45;1' '31;45;5' '31;45;7' '31;45;22' '31;45;25' '31;45;27' '31;45' '31;46;1' '31;46;5' '31;46;7' '31;46;22' '31;46;25' '31;46;27' '31;46' '31;47;1' '31;47;5' '31;47;7' '31;47;22' '31;47;25' '31;47;27' '31;47' '31;1' '31;5' '31;7' '31;22' '31;25' '31;27' '31' '32;40;1' '32;40;5' '32;40;7' '32;40;22' '32;40;25' '32;40;27' '32;40' '32;41;1' '32;41;5' '32;41;7' '32;41;22' '32;41;25' '32;41;27' '32;41' '32;42;1' '32;42;5' '32;42;7' '32;42;22' '32;42;25' '32;42;27' '32;42' '32;43;1' '32;43;5' '32;43;7' '32;43;22' '32;43;25' '32;43;27' '32;43' '32;44;1' '32;44;5' '32;44;7' '32;44;22' '32;44;25' '32;44;27' '32;44' '32;45;1' '32;45;5' '32;45;7' '32;45;22' '32;45;25' '32;45;27' '32;45' '32;46;1' '32;46;5' '32;46;7' '32;46;22' '32;46;25' '32;46;27' '32;46' '32;47;1' '32;47;5' '32;47;7' '32;47;22' '32;47;25' '32;47;27' '32;47' '32;1' '32;5' '32;7' '32;22' '32;25' '32;27' '32' '33;40;1' '33;40;5' '33;40;7' '33;40;22' '33;40;25' '33;40;27' '33;40' '33;41;1' '33;41;5' '33;41;7' '33;41;22' '33;41;25' '33;41;27' '33;41' '33;42;1' '33;42;5' '33;42;7' '33;42;22' '33;42;25' '33;42;27' '33;42' '33;43;1' '33;43;5' '33;43;7' '33;43;22' '33;43;25' '33;43;27' '33;43' '33;44;1' '33;44;5' '33;44;7' '33;44;22' '33;44;25' '33;44;27' '33;44' '33;45;1' '33;45;5' '33;45;7' '33;45;22' '33;45;25' '33;45;27' '33;45' '33;46;1' '33;46;5' '33;46;7' '33;46;22' '33;46;25' '33;46;27' '33;46' '33;47;1' '33;47;5' '33;47;7' '33;47;22' '33;47;25' '33;47;27' '33;47' '33;1' '33;5' '33;7' '33;22' '33;25' '33;27' '33' '34;40;1' '34;40;5' '34;40;7' '34;40;22' '34;40;25' '34;40;27' '34;40' '34;41;1' '34;41;5' '34;41;7' '34;41;22' '34;41;25' '34;41;27' '34;41' '34;42;1' '34;42;5' '34;42;7' '34;42;22' '34;42;25' '34;42;27' '34;42' '34;43;1' '34;43;5' '34;43;7' '34;43;22' '34;43;25' '34;43;27' '34;43' '34;44;1' '34;44;5' '34;44;7' '34;44;22' '34;44;25' '34;44;27' '34;44' '34;45;1' '34;45;5' '34;45;7' '34;45;22' '34;45;25' '34;45;27' '34;45' '34;46;1' '34;46;5' '34;46;7' '34;46;22' '34;46;25' '34;46;27' '34;46' '34;47;1' '34;47;5' '34;47;7' '34;47;22' '34;47;25' '34;47;27' '34;47' '34;1' '34;5' '34;7' '34;22' '34;25' '34;27' '34' '35;40;1' '35;40;5' '35;40;7' '35;40;22' '35;40;25' '35;40;27' '35;40' '35;41;1' '35;41;5' '35;41;7' '35;41;22' '35;41;25' '35;41;27' '35;41' '35;42;1' '35;42;5' '35;42;7' '35;42;22' '35;42;25' '35;42;27' '35;42' '35;43;1' '35;43;5' '35;43;7' '35;43;22' '35;43;25' '35;43;27' '35;43' '35;44;1' '35;44;5' '35;44;7' '35;44;22' '35;44;25' '35;44;27' '35;44' '35;45;1' '35;45;5' '35;45;7' '35;45;22' '35;45;25' '35;45;27' '35;45' '35;46;1' '35;46;5' '35;46;7' '35;46;22' '35;46;25' '35;46;27' '35;46' '35;47;1' '35;47;5' '35;47;7' '35;47;22' '35;47;25' '35;47;27' '35;47' '35;1' '35;5' '35;7' '35;22' '35;25' '35;27' '35' '36;40;1' '36;40;5' '36;40;7' '36;40;22' '36;40;25' '36;40;27' '36;40' '36;41;1' '36;41;5' '36;41;7' '36;41;22' '36;41;25' '36;41;27' '36;41' '36;42;1' '36;42;5' '36;42;7' '36;42;22' '36;42;25' '36;42;27' '36;42' '36;43;1' '36;43;5' '36;43;7' '36;43;22' '36;43;25' '36;43;27' '36;43' '36;44;1' '36;44;5' '36;44;7' '36;44;22' '36;44;25' '36;44;27' '36;44' '36;45;1' '36;45;5' '36;45;7' '36;45;22' '36;45;25' '36;45;27' '36;45' '36;46;1' '36;46;5' '36;46;7' '36;46;22' '36;46;25' '36;46;27' '36;46' '36;47;1' '36;47;5' '36;47;7' '36;47;22' '36;47;25' '36;47;27' '36;47' '36;1' '36;5' '36;7' '36;22' '36;25' '36;27' '36' '37;40;1' '37;40;5' '37;40;7' '37;40;22' '37;40;25' '37;40;27' '37;40' '37;41;1' '37;41;5' '37;41;7' '37;41;22' '37;41;25' '37;41;27' '37;41' '37;42;1' '37;42;5' '37;42;7' '37;42;22' '37;42;25' '37;42;27' '37;42' '37;43;1' '37;43;5' '37;43;7' '37;43;22' '37;43;25' '37;43;27' '37;43' '37;44;1' '37;44;5' '37;44;7' '37;44;22' '37;44;25' '37;44;27' '37;44' '37;45;1' '37;45;5' '37;45;7' '37;45;22' '37;45;25' '37;45;27' '37;45' '37;46;1' '37;46;5' '37;46;7' '37;46;22' '37;46;25' '37;46;27' '37;46' '37;47;1' '37;47;5' '37;47;7' '37;47;22' '37;47;25' '37;47;27' '37;47' '37;1' '37;5' '37;7' '37;22' '37;25' '37;27' '37' '40;1' '40;5' '40;7' '40;22' '40;25' '40;27' '40' '41;1' '41;5' '41;7' '41;22' '41;25' '41;27' '41' '42;1' '42;5' '42;7' '42;22' '42;25' '42;27' '42' '43;1' '43;5' '43;7' '43;22' '43;25' '43;27' '43' '44;1' '44;5' '44;7' '44;22' '44;25' '44;27' '44' '45;1' '45;5' '45;7' '45;22' '45;25' '45;27' '45' '46;1' '46;5' '46;7' '46;22' '46;25' '46;27' '46' '47;1' '47;5' '47;7' '47;22' '47;25' '47;27' '47' '1' '5' '7' '22' '25' '27' )

alphabet_soup=( ABC DEF GHI JKL MNO PQR STU VWX YYZ )

color_array_count=${#color_array[@]}
alphabet_soup_count=${#alphabet_soup[@]}

echo "Number of color code combinations in array = $color_array_count"

alpha_count=0
for ((x=0; x<$color_array_count; x++))
do
if [ $alpha_count -gt 9 ]
then
alpha_count=0
fi
echo -ne "\033[${color_array[${x}]}m${alphabet_soup[${alpha_count}]}\033[0m"
((alpha_count++))
done
echo


, Mike