[Bash shell] Display the output with color

In bash shell, we have multiple ways to display the output, but do u know how to do this with color ?
Like this :
Image 8

It’s not so hard to do, the only thing we need to understand is  : ANSI escape code

An example of correspondence between the code and the color :

Black        0;30     Dark Gray     1;30
Red          0;31     Light Red     1;31
Green        0;32     Light Green   1;32
Brown/Orange 0;33     Yellow        1;33
Blue         0;34     Light Blue    1;34
Purple       0;35     Light Purple  1;35
Cyan         0;36     Light Cyan    1;36
Light Gray   0;37     White         1;37

The example in bash shell

#!/bin/bash

NC='\033[0m' 		# No color
RED='\033[0;31m'	# red
PURPLE='\033[0;35m'	# purple

printf "My ${PURPLE}name${NC} is ${RED}BO${NC} \n"

Do this in shell, and launch your script, you will get the same from the image in the beginning of this article.

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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