echo colored text in a shell script?

As part of a shell script I'm working on, I want to be able to print colored (preferably red) text to the Terminal window, to provide warnings to the user.

How can I code in echoing colored text?

(I did some googling and couldn't find anything that seemed to apply to what I want to do.)

Any help would be appreciated!

Thanks in advance!

17" iMac 2GHz Intel Core 2 Duo 2.5GB RAM 160GB hard drive, Mac OS X (10.5.5), iPod 5th gen (with video) 30GB.

Posted on Nov 19, 2008 4:31 PM

Reply
7 replies

Nov 19, 2008 5:36 PM in response to joshz

Color text is done by embedded escape characters in the text. This makes it really hard to visualize when typing, but it's possible.

The sequence starts with "Ctrl-v ESC [", that is type Ctrl-v followed by the Escape key (which will appear as ^[ ) followed by left bracket. You then specify one or two numbers in the form:

ab

where a = 3 to define the foreground (text) color and a=4 to set the background color, and b is one of the following:

0 = black
1 = red
2 = green
3 = yellow
4 = blue
5 = magenta
6 = cyan
7 = white

So 30;42 would print red text (31) on a green background (42), and 36;40 would print cyan text on a black background, and so on.

You then print 'm' followed by the text. You should follow the string by ^[[0m to reset the colors back to default (otherwise the same color scheme would continue to be used for all subsequent text.

So, as an example, you could:

echo "^[[31;43mWarning^[[0m"


(remembering that ^[ is entered as 'Ctrl-v ESC')

This would print 'Warning' in red text on a yellow background.

(There is one further set of attributes you can use to print bold, underlined or flashing text - just try prepending a single digit before the colors to see what effects you can get)

Just bear in mind that different shells and different terminals may interpret the codes differently (or have a different set altogether - the above is based on the ANSI standard, but VT100 works differently.

Nov 19, 2008 5:51 PM in response to joshz

The following is a shell script that displays the terminal colors and the escape sequences used to generate them:

#!/bin/sh
#-----------------------------------------------------------------------------
# colors.esc - display the terminal colors and the escape sequences that
# generate them. "Blink" is _NOT_ shown, as it is a very
# annoying effect, and should be avoided at all costs in normal
# daily uses.
#
# Usage: colors.esc [12 character or less display string]
#-----------------------------------------------------------------------------
# Bob Harris
#-----------------------------------------------------------------------------
E=$(printf ' ') # escape <E>
R="${E}[0m" # reset <E>[0m
typeset dsp_str=""
typeset dsp_txt
typeset fg_text
typeset bg_text
if [[ $# != 0 ]]; then
dsp_str=$(printf "%12.12s" "$*")
fi
color=(Black Red Green Yellow Blue Magenta Cyan White)
for bg in "" 0 1 2 3 4 5 6 7 # for each background color
do
b=$bg && [[ X$b != X ]] && b="4$b;" # deal with no background color ""
echo " ------------ ----bold---- -underline-- --reverse---"
for fg in 0 1 2 3 4 5 6 7 # for each foreground color
do
f="3$fg" # setup foreground color
line=""
for a in "" 1 4 7 # for each attribute
do
[[ X$a != X ]] && a=";$a" # deal with no attribute ""
if [[ -z "$dsp_str" ]]; then
dsp_txt=$(printf "%12s" "<E>[${b}${f}${a}m") # build esc text
else
dsp_txt="$dsp_str" # use supplied text
fi
line="${line}${E}[${b}${f}${a}m${dsp_txt}${R} " # build entry
done
fg_text=$(printf "%-7s" ${color[$fg]}) # translate foreground color
bg_text=$(printf "%-9s" "Bg${color[$bg]}") # translate background color
[[ X$b = X ]] && bg_text=$(printf "%-9s" " ") # no bckgnd color
line="$bg_text $fg_text $line" # build final display line
echo "${line% }" # display the colorized line
done
done
echo " ------------ ----bold---- -underline-- --reverse---"
bg_text=$(printf "%-9s" " ")
fg_text=$(printf "%-7s" "Reset")
dsp_txt=$(printf "%12s" "<E>[0m")
line="$bg_text $fg_text $dsp_txt" # build final display line
echo "${line% }"

Nov 19, 2008 6:26 PM in response to joshz

The pico help ^G says:
pressing Esc twice and then typing a three-digit decimal number from 000 to 255 will enter the character with the corresponding value.

Although I really do not use pico, so your mileage may vary.

Or you can use the following in a shell script for escape:

printf " "
# or
echo -e " "

for example:

echo -e " [31mRed [m"
# or
printf " [31m"
printf "Red text that you want to see"
printf " [m"
printf " "

One simplification would be to create variables with the escape codes in the variable:

RED=$(printf " [31m")
RESET=$(printf " [m")
echo "${RED}Some Red Text${RESET}"

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

echo colored text in a shell script?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.