Search Windows and Linux Networking

Tuesday, March 18, 2014

How to print or display colorful text on screen in terminal or in bash scripting with echo command

How to print or display colorful text on screen in terminal    


 It is always good practice to show your script related information in colorful text in terminal to highlight or attention to the user. for example you can give output of script test as successful or fail in Green and red color, you can also display error message in Red color so user can attention for result\Output.

There are many method to do this for example with help of sed , awk , echo and may more. I will show you how to display text in colorful output with the help of echo command.

suppose you wanted to display only Hello world in Red color. you can do this with echo like follow

echo -e "\e[0;31m Hello World \e[om"

or
echo -e  "\033[31m Hello World \033[0m"
Hello World

suppose you wanted to display only Hello in Red color and World in Blue then your command look like follow

echo -e "\e[0;31m Hello \e[0;34m World \e[0m"
or
echo -e  "\033[31m Hello \033[34m World \033[0m"
Hello World


For Bold text Hello  World command looks:

echo -e "\e[1m Hello World\e[0m"
or
echo -e "\033[1m Hello World\033[0m"
Hello World

to underline the Hello world command is as follow:

echo -e "\e[4m Hello World\e[0m"
 Hello World
echo -e "\e[4;31m Hello World\e[0m"
 Hello World

Following are some other color code you wanted to use in your script.


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



 

No comments:

Post a Comment