kedar.nitty-witty.com
Sunday August 1st 2010

How to echo colored text in linux shell script

Try following command on you bash:
bash $] tput setaf 1
You will see your text color will turn red.
To reset it you can type following command to make text color white
bash $] tput setaf 7

or to reset everything again text modes
bash $] tput sgr0

Same things you can use in your bash script to colorize output.

Consider following sample bash script for colorize echo:


#try.sh
txtrst=$(tput sgr0) # Text reset
txtred=$(tput setaf 1) # Red
echo “Welcome to ${txtred} kedar.nitty-witty.com ${txtrst}!”

Output::

colored-shell-script

Other variables you can define as follows:
txtgrn=$(tput setaf 2) # Green
txtylw=$(tput setaf 3) # Yellow
txtblu=$(tput setaf 4) # Blue
txtpur=$(tput setaf 5) # Purple
txtcyn=$(tput setaf 6) # Cyan
txtwht=$(tput setaf 7) # White
txtrst=$(tput sgr0) # Text reset
Following are the tput details further:
tput setab [1-7] : Set a background colour using ANSI escape
tput setb [1-7] : Set a background colour
tput setaf [1-7] : Set a foreground colour using ANSI escape
tput setf [1-7] : Set a foreground colour

tput Text Mode Capabilities:

tput bold : Set bold mode
tput dim : turn on half-bright mode
tput smul : begin underline mode
tput rmul : exit underline mode
tput rev : Turn on reverse mode
tput smso : Enter standout mode (bold on rxvt)
tput rmso : Exit standout mode
tput sgr0 : Turn off all attributes (doesn’t work quite as expected)


Bookmark and Share

Related posts:

  1. MyDumpSplitter-Extract tables from Mysql dump-shell script A lot of articles have been written on splitting mysqldump...
  2. Simple Shell Script to Monitoring Disk Space on a Linux Machine This article explain a way to get a mail as...
  3. Linux Shell Commands – quick how-tos Following are linux commands, tricks for doing regular tasks. Rename...

Leave a Reply