Bash – Colored text output with tput

Introduction

I was looking for a way to highlight text output from a bash script. Last year when I did my final project building a small device that could call back to a main server independently on where it was placed. Then a user was able to login locally on main server to access the device.

All code was written is bash and ash, but is was difficult to see the different output the automated code send out. So I need to could customize the output in color and font size so it could be made easy and more understandable.

I have been having some  to produce terminal control codes for all kinds of things.

Usage

tput,  reset  -  initialize  a  terminal or query terminfo
       database

So there has to be a “tput” to start the format of the text into color or other formatting wishers followed by a reset to return to default text

Like:

echo  <tput> text <reset>
or
echo  <tput> <tput><tput>  text <reset>

Small exemple is to print out a red piece of text

tput setaf 1; echo “Here is red text”

Use ; instead of && so if there are any tput errors the text still be shown.

Shell variables

Another way is to use variables.  I myself prefer to use variables when working with tput. It is easier for me to remember a noun.

green=`tput setaf 2`
blue=`tput setaf 4`
reset=`tput sgr0`
echo “${green}green text ${blue}blue text${reset}”

tput produces character sequences that are interpreted by the terminal as having a special meaning. They will not be shown themselves. Note that they can still be saved into files or processed as input by programs other than the terminal.

Command substitution

Skærmbillede 2015-03-29 21.21.14
Example from Debian

echo “$(tput setaf 1)Color text $(tput setab 3)and custom background$(tput sgr 0)”

The example shows a one-liner that echo out a desired text in red with a yellow background.

Colors in Foreground & background

Colour commands

tput setab [1-7]  this sets the background color
tput setaf [1-7]  this sets the foreground color

Colous is as follows:
Num Color is defined by Red Green Blue

0 black COLOR_BLACK 0,0,0
1 red COLOR_RED 1,0,0
2 green COLOR_GREEN 0,1,0
3 yellow COLOR_YELLOW 1,1,0
4 blue COLOR_BLUE 0,0,1
5 magenta COLOR_MAGENTA 1,0,1
6 cyan COLOR_CYAN 0,1,1
7 white COLOR_WHITE 1,1,1

Text mode commands

tput bold – Select bold mode
tput dim – Select dim (half-bright) mode
tput smul – Enable underline mode
tput rmul – Disable underline mode
tput rev – Turn on reverse video mode
tput smso – Enter standout (bold) mode
tput rmso – Exit standout mode

tput accepts scripts containing one command per line, which are executed in order before tput exits.

Avoid temporary files by echoing a multiline string and piping it:

echo -e “setf 7\nsetb 1” | tput -S # set fg white and bg red

See also for more information

LinuxCommand.org

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.