How to color a text/string in linux bash shell & make it blink ?

An interesting thing to do when creating shell scripts is to know how to color some text/output and blink it.

Today in this article , we will look how to achieve this.

I.  Color a string
To color a string, we need to get ASCII codes w.r.t to colors present. To get these codes, we can refer to below url:

https://en.wikipedia.org/wiki/ANSI_escape_code

Below color scheme example is taken from page: https://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux.

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

 

Explanation
1. “\033[“. is the escape sequence for color code.
2. 0;32 —> Green color code
3. 0 –> No Color/ Default Black color code

 

Example to achieve color Text

### Create a script file like below.
bash-3.2# cat test1.sh
GREEN='\033[0;32m'
NOCOLOR='\033[0m'
echo -e "Welcome to $GREEN NGELINUX.com"

### Execute the script and see the magic.
### The prompt becomes green in color alongwith output.
bash-3.2# ./test1.sh
Welcome to NGELINUX.com
bash-3.2#

### Make the prompt black in color back.
bash-3.2# echo -e '\033[0m'

bash-3.2#

Ngelinux.com color text tutorial

 

II. Blink a text

bash-3.2# echo -e " \033[5mTitle of the Program\033[0m"
 Title of the Program
bash-3.2# 

 

III. Blink and color a string

### Shows green color and blinks.
bash-3.2# echo -e " \033[32;5mTitle of the Program\033[0m"
 Title of the Program
bash-3.2# 




References:

https://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux
https://en.wikipedia.org/wiki/ANSI_escape_code

Leave a Reply

avatar
  Subscribe  
Notify of