how to ANSI Color Sequences using shell script

Posted on 7:03 PM by Bharathvn

Although you probably don't realize it, your standard terminal application supports different styles of presenting text. Quite a few variations are possible, whether you'd like to have certain words in your script displayed in bold, or even in red against a yellow background. However, working with ANSI (American National Standards Institute) sequences to represent these variations can be difficult because these sequences are quite user unfriendly. Therefore, this script fragment creates a set of variables, whose values represent the ANSI codes, that can turn on and off the various color and formatting display capabilities.

The Code

# ANSI Color -- Use these variables to make output in different colors
# and formats. Color names that end with 'f' are foreground (text) colors,
# and those ending with 'b' are background colors.

esc="\033" # if this doesn't work, enter an ESC directly

blackf="${esc}[30m"; redf="${esc}[31m"; greenf="${esc}[32m"
yellowf="${esc}[33m" bluef="${esc}[34m"; purplef="${esc}[35m"
cyanf="${esc}[36m"; whitef="${esc}[37m"

blackb="${esc}[40m"; redb="${esc}[41m"; greenb="${esc}[42m"
yellowb="${esc}[43m" blueb="${esc}[44m"; purpleb="${esc}[45m"
cyanb="${esc}[46m"; whiteb="${esc}[47m"

boldon="${esc}[1m"; boldoff="${esc}[22m"
italicson="${esc}[3m"; italicsoff="${esc}[23m"
ulon="${esc}[4m"; uloff="${esc}[24m"
invon="${esc}[7m"; invoff="${esc}[27m"


How It Works
If you're used to HTML, you might be a bit baffled by the way these sequences work. In HTML, you open and close modifiers in opposite order, and you must close every modifier you open. So to create an italicized passage within a sentence displayed in bold, you'd use the following HTML:

this is in bold and this is italics within the bold

Closing the bold tag without closing the italics wreaks havoc and can crash some Web browsers. But with the ANSI color sequences, some modifiers replace the previous modifier, and all modifiers are closed with a single reset sequence. With ANSI sequences, you must make sure to output the reset sequence after colors and to use the "off" feature for anything you turn on. Using the variable definitions in this script, you would write the previous sequence as follows:

${boldon}this is in bold and ${italicson}this is
italics${italicsoff}within the bold${reset}

Running the Script
To run this script, we'll need to initialize all the ANSI sequences and then output a few echo statements with different combinations of color and type effect:


cat << EOF
${yellowf}This is a phrase in yellow${redb} and red${reset}
${boldon}This is bold${ulon} this is italics${reset} bye bye
${italicson}This is italics${italicsoff} and this is not
${ulon}This is ul${uloff} and this is not
${invon}This is inv${invoff} and this is not
${yellowf}${redb}Warning I ${yellowb}${redf}Warning II${reset}

The Results
The appearance of the results isn't too thrilling in this book, but on a display that supports these color sequences it definitely catches your attention:

This is a phrase in yellow and red
This is bold this is italics bye bye
This is italics and this is not
This is ul and this is not
This is inv and this is not
Warning I Warning II

Hacking the Script
When using this script, you may see something like the following:


If you do, the problem might be that your terminal or window doesn't support ANSI color sequences, but it also might simply be that the \033 notation for the all-important esc variable isn't understood. To remedy the latter problem, open up the script in the vi editor or your favorite editor, replace the \033 sequence with a ^V sequence, and then press the ESC key. You should see ^[ displayed, so the results on screen look like esc="^[" and all should work fine.

If, on the other hand, your terminal or window doesn't support ANSI color sequences, you might want to upgrade so that you can add colorized and type-face-enhanced output to your other scripts.