Color text output on bash scripts

Users who have been using Linux for awhile often learn that creating a basic script is a good way to run multiple, often-repeated commands. Adding a little color to scripts can additionally provide nice feedback. This can be done in a fairly straight-forward way by using the tput command.

A common way of doing this is to define the colors that tput can produce by putting them at the beginning of the bash script:

# scriptname - description of script

# Text color variables
txtund=$(tput sgr 0 1)          # Underline
txtbld=$(tput bold)             # Bold
bldred=${txtbld}$(tput setaf 1) #  red
bldblu=${txtbld}$(tput setaf 4) #  blue
bldwht=${txtbld}$(tput setaf 7) #  white
txtrst=$(tput sgr0)             # Reset
info=${bldwht}*${txtrst}        # Feedback

When writing new scripts using templates with these variables already defined can quicken the creation process and help keep scripts organized (my Bash Templates).

If just needing to use tput colors for specific instances this script can display the tput definitions and their corresponding possibilities:

# tputcolors

echo -e "$(tput bold) reg  bld  und   tput-command-colors$(tput sgr0)"

for i in $(seq 1 7); do
  echo " $(tput setaf $i)Text$(tput sgr0) $(tput bold)$(tput setaf $i)Text$(tput sgr0) $(tput sgr 0 1)$(tput setaf $i)Text$(tput sgr0)  \$(tput setaf $i)"

echo ' Bold            $(tput bold)'
echo ' Underline       $(tput sgr 0 1)'
echo ' Reset           $(tput sgr0)'

To use additional colors see: Color Output on Bash Scripts (Advanced).

12 thoughts on “Color text output on bash scripts

  1. grim

    That’s what I’m using for colors in .bashrc

    That’s a bit less process-spawning :)

    # shell.names:
    # Names for simplicity
    # Attributes
    c_reset=`escape 0m` ;
    c_bold_on=`escape 1m` ; c_bold_off=`escape 22m`
    c_blink_on=`escape 5m` ; c_blink_off=`escape 25m`
    c_reverse_on=`escape 7m`; c_reverse_off=`escape 27m`
    # Foreground colors
    cf_default=`escape 39m` ;
    cf_black=`escape 30m` ; cf_red=`escape 31m`
    cf_green=`escape 32m` ; cf_brown=`escape 33m`
    cf_blue=`escape 34m` ; cf_magenta=`escape 35m`
    cf_cyan=`escape 36m` ; cf_white=`escape 37m`
    # Background colors
    cb_default=`escape 49m`
    cb_black=`escape 40m` ; cb_red=`escape 41m`
    cb_green=`escape 42m` ; cb_brown=`escape 43m`
    cb_blue=`escape 44m` ; cb_magenta=`escape 45m`
    cb_cyan=`escape 46m` ; cb_white=`escape 47m`
    # Artefacts

    Just my 2 copecks :) and just a different approach

  2. Pingback: Quora

  3. eBop

    The tput colors bash script above doesn’t underline on my system macosx10.6.8.
    i know this is a linux forum just thought i’d ask if you know why.

  4. Todd Partridge (Gen2ly) Post author

    Apple uses BSD developed tools that are basically forks of earlier versions of Linux tools. Generally, because GNU/Linux tools were used more they got better developed, so the transition between many tools with the same name in BSD’s and Linux’s can be different.

    1. Helpful Soul

      This is false. GNU is a re-implementation of old Sys V UNIX userland (look up what GNU means) and the BSDs are old UNIX continued.

  5. MattDMo

    On a 256-color terminal like xterm, PuTTY, or (make sure to set the right config options like `export TERM=xterm-256color`), you can replace the 7 in `for i in $(seq 1 7); do` in the `tputcolors` script with 255 to get all the possible colors. It makes for a much more colorful time!

  6. Pingback: Color Output on Bash Scripts | Linux Tidbits | Das OSX Log

Leave a Reply

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

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google 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 )

Connecting to %s