Click to See Complete Forum and Search --> : Shell Scripts and Colored Output


aph3x
08-28-2001, 02:04 AM
can anyone tell me how to "colorize" text in a shell script? im thinking i need to use ascii color codes, i just dont know how to implement them...

something like:

echo "This string is red"

and have the string colored red when it is written to stdout

danke!

The Kooman
08-28-2001, 04:35 AM
Originally posted by aph3x:
<STRONG>can anyone tell me how to "colorize" text in a shell script? im thinking i need to use ascii color codes, i just dont know how to implement them...</STRONG>

I haven't played much with color so I can't really give much (useful) details, but the few pointers that I can give are:
man tput
man terminfo - the color handling section


Basically you'd take the capability codes from man terminfo and give it to tput to do what you want. E.g.
echo `tput smso`This will be hilighted`tput rmso`
will, as it says, print the message in highlighted mode. Note, those are back-quotes and not normal quotes.

HTH

TheLinuxDuck
08-28-2001, 10:15 AM
aph3x:

As long as you're in *nix, you can print ansi color sequences using the CL binary 'printf'. Here is a list of ansi escape codes (http://www.bluesock.org/~willg/dev/ansi.html). Here is an example of using ansi escape codes:


#!/bin/bash

printf "\x1b[2J\x1b[1;1H"
printf "Printed at the top of the screen, after a clear screen"

printf "\x1b[5;5H"
printf "Printed at 5,5"

printf "\x1b[10;10H"
printf "Printed at 10,10"

exit

aph3x
08-28-2001, 02:38 PM
werrrd!

thank you both for the suggestions... now i know how to use color in both C programs and shell scripts! you always get more than you expect at LNO :)

cheers

TheLinuxDuck
08-28-2001, 04:10 PM
The ansi escape sequences are nice because they can be use in any nix programming language (that I know), and also, if you're using a DOS machine and have ansi.sys loaded, they work too.

Ben Briggs
08-28-2001, 05:59 PM
Originally posted by aph3x:
<STRONG>werrrd!

thank you both for the suggestions... now i know how to use color in both C programs and shell scripts! you always get more than you expect at LNO :)

cheers</STRONG>

I wrote an example for color escape sequences for codeexamples.org (http://www.codeexamples.org). I wrote one for C and Python (same results, slightly different code).