Color Output on Bash Scripts (Advanced)

I talked in a previous post about basic bash script colored output using the tput command. The tput command works for basic coloring (providing seven colors to choose from) but ANSI also provides a 256 color palette.

Note: Not all terminals support ANSI, but most do.

ANSI color coding is in this form:


The ANSI sequence: {ESC}[{attr};{bg};{256colors};{fg}m

{ESC} or \033 represents the ANSI escape-sequence. {attr} represents the outputs attributes (properties such as blinking and bold text), {fg} is the foreground color, {bg} is the background color, m means the sequence ends.

Note: The escape-sequence \033 works fine but at times you might have to use \e.

An example:

echo -e "My favorite color is \033[38;5;148mYellow-Green\033[39m"

The variable -e is required because echo doesn’t normally interpret backslashes and 033[39m tells bash to end the seqeunce. The 38 Value will use no background color. Notice too that I omitted the attribution value which if isn’t used will use the default (regular text) value.

To get color values a good program is colortest.

colortest -w

Colortest will show the ANSI color value to the corresponding hex value. Then just insert the ANSI value into the either the foreground or background value.
There’s also a program called conv-rgb2xterm that a hex value can be put in and it will give and ANSI sequence for the foreground color.


About Todd Partridge (Gently)

Good times, good people, good fun.

Posted on 2008-08-13, in Command Line, Linux, Script. Bookmark the permalink. 3 Comments.

  1. hi! Pretty much thanks for the scripts!
    I’ve found urxvt to not support all 256 colors, only 88 :(
    Time to change software it seems…

    Anyway, about the colors I found quite recently that ANSI sequences can be used almost anywhere.

    One important for me is /etc/motd and my.cnf.
    You just compose the bare text and then add colors with ANSI-sequences.

  2. Hi to every , for the reason that I am in fact keen of
    reading this web site’s post to be updated on a regular basis.

    It includes pleasant material.

  1. Pingback: Output colorato bash scripting

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

Join 58 other followers

%d bloggers like this: