nerdstock.org

PROJECTS | MANUALS | OTHER
English

Using color with BASH, colorize your scripts

How do you use color in you BASH scripts, that's the problem this page tries to solve. It's a howto and it provides a sample script to include in your BASH script. All to make it easier to use color in your BASH scripts. Use of the scripts is at your own risk.


Show all colors

Using color is quite easy, you just use

echo
and the correct color codes. I found a script on the Internet that shows all the colors, here it is:
for h in 0 1;
do
echo -e "\\033[0;${h}m\\c"
case $h in
0) echo -e "Normal\\c" ;;
1) echo -e "High\\c" ;;
esac
echo -e " intensity foreground (background color in parentheses)\\033[m"
for f in 0 1 2 3 4 5 6 7; do
for b in 0 1 2 3 4 5 6 7 8; do
echo -e "\\033[${h};3${f}\\c"
if [ $b -lt 8 ]; then
echo -e ";4${b}m\\c"
else
echo -e "m\\c"
fi

case $f in  0)
echo -e " black \\c" ;;
1) echo -e " red \\c" ;;
2) echo -e " green \\c" ;;
3) echo -e " yellow\\c" ;;
4) echo -e " blue \\c" ;;
5) echo -e "magenta\\c" ;;
6) echo -e " cyan \\c" ;;
7) echo -e " white \\c" ;;
esac

case $b in
8) echo -e "\\033[m" ;;
*) echo -e " \\033[m \\c" ;;
esac

done
done
echo -e "\\033[${h}m\\c"
for b in 0 1 2 3 4 5 6 7 8; do
case $b in
0) echo -e "(black) \\c" ;;
1) echo -e " (red) \\c" ;;
2) echo -e "(green) \\c" ;;
3) echo -e "(yellow) \\c" ;;
4) echo -e " (blue) \\c" ;;
5) echo -e "(magenta)\\c" ;;
6) echo -e " (cyan) \\c" ;;
7) echo -e "(white) \\c" ;;
8) echo -e " (none)\\c" ;;

esac
done
echo -e "\\033[m\\n"
done

To use it, save it to a file (mine is called terminal_color),

chmod +x terminal_color
and run it:
./terminal_color
or
bash terminal_color
This will give you an overview of the colors you can use.

Using color in your own scripts

Now, we've seen the colors, how do we use them in our own scripts? Simple, you just use echo and the correct color code:

echo -e \\E[33m \\033[1m kip
The text 'kip' will be printed in yellow.You need the -e option to 'enable interpretation of backslash escapes' (so also
man echo
). The '\\E[33m \\033[1m' part triggers the color code. The first part ('\\E[33m') triggers the color yellow, the second part (\\033[1m) increases the intensity, so you end up with a brighter color yellow. So by echoing the correct codes you can use color. But to make life easier i've made a small script to make it easier to use color:

declare -a colors

colors[1]='\E[31m\033[1m'
colors[2]='\E[34m\033[1m'
colors[3]='\E[37m\033[1m'
colors[4]='\E[33m\033[1m'
colors[5]='\E[30m\033[1m'
colors[6]='\E[32m\033[1m'
colors[7]='\E[35m\033[1m'
colors[8]='\E[36m\033[1m'

declare -a color_names
color_names='red blue white yellow black green magenta cyan'

export reset_screen='echo -n -e \033[0m'

#
# FUNCTIONS
#

cecho () { # Color-echo Argument $1 = message Argument $2 = color $3 = no break

    default_msg="No message passed."

    message=${1:-$default_msg}   # Defaults to default message.

    use_color=''

    if [ "$2" == "" ];then
        use_color=${colors[3]}
    else
        i=1
        for color in $color_names;do
            if [ "$color" == "$2" ]; then
                use_color=${colors[${i}]}
            fi
            ((i=${i}+1))
        done
    fi
    if [ "$use_color" == "" ];then
        echo 'unknown color';
    fi

    echo_cmd='echo'
    if [ "$3" == "false" ];then
        echo_cmd='echo -n'
    fi
    echo -n -e "$use_color"
    $echo_cmd "$message"

    #
# INIT VARS
#
declare -a colors

colors[1]='\E[31m\033[1m'
colors[2]='\E[34m\033[1m'
colors[3]='\E[37m\033[1m'
colors[4]='\E[33m\033[1m'
colors[5]='\E[30m\033[1m'
colors[6]='\E[32m\033[1m'
colors[7]='\E[35m\033[1m'
colors[8]='\E[36m\033[1m'

declare -a color_names
color_names='red blue white yellow black green magenta cyan'

export reset_screen='echo -n -e \033[0m'

#
# FUNCTIONS
#

cecho () { # Color-echo Argument $1 = message Argument $2 = color $3 = no break

    default_msg="No message passed."

    message=${1:-$default_msg}   # Defaults to default message.

    use_color=''

    if [ "$2" == "" ];then
        use_color=${colors[3]}
    else
        i=1
        for color in $color_names;do
            if [ "$color" == "$2" ]; then
                use_color=${colors[${i}]}
            fi
            ((i=${i}+1))
        done
    fi
    if [ "$use_color" == "" ];then
        echo 'unknown color';
    fi

    echo_cmd='echo'
    if [ "$3" == "false" ];then
        echo_cmd='echo -n'
    fi
    echo -n -e "$use_color"
    $echo_cmd "$message"
    
    $reset_screen
    return
}

The first part defines all the colors you can use. If you want to add a color simply add: colors[9]='YOURCOLORCODE' replace YOURCOLORCODE with the new color code you want to use. The array color_names defines references to the color codes. So if you have added a new color you should also add a reference to it, simply add it to the array: color_names='red blue white yellow black green magenta cyan YOURCOLOR'. The variable reset_screen is use to reset the color to the default one. Next in the script is the function: cecho. cecho echo's text in a specified color, eg:

cecho 'kip' yellow
This will output the text kip in yellow. If you add false as a third parameter
cecho
will not print a newline. So you can use cecho also to highlight in work in a sentence:
cecho 'Error!' red false;echo ' something evil has happend'
This will echo 'Error! something evil has happend' on 1 line with 'Error!' in red.

If you want to use the script in you own scripts just do the following: - save the script, for instance to

color_functions.sh
- chmod +x color_functions - in you script just put
source color_functions.sh
That's all! #TODO add a section for a colord prompt

Responses are welcomed:
arnoud[at]nerdstock.org
Creative Commons License
http://creativecommons.org/licenses/by-nc-sa/3.0/nl/deed.en

 


-i *.nerdstock.org/*
Nerds don't google, they grep.

FaciliPro
http://facilipro.nl
Dannik
http://dannik.nl
Setara
http://setara.org
OhReally.nl
http://OhReally.nl
HoudtVan.je
http://www.houdtvan.je
Ads by Nerdstock.org

Link: Trademarks used at this site