Color 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).

About Todd Partridge (Gently)

Good times, good people, good fun.

Posted on 2008-08-11, in Command Line, Linux, Script. Bookmark the permalink. 18 Comments.

  1. nice tip :)

  2. 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

  3. damn, forget the main part :-D

    echo “\[33[$1\]”

  4. Grim,

    Don’t you have to use “echo -n” to suppress the newline?

  5. 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.

  6. 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.

    • 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.

  7. Kannan Manickam

    Great to know that we can output colors in bash. Thanks!

  8. Admiring the time and effort you put into your website
    and in depth information you provide. It’s good to come across a blog every once in a while that isn’t the
    same unwanted rehashed information. Fantastic read!
    I’ve bookmarked your site and I’m adding your RSS feeds to
    my Google account.

  9. Hey There. I found your blog using msn. This is an extremely well written
    article. I will be sure to bookmark it and come back to read
    more of your useful info. Thanks for the post. I’ll definitely return.

  10. 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!

  11. Magnificent beat ! I wish to apprentice while
    you amend your website, how could i subscribe for a blog website?
    The account helped me a acceptable deal. I had been tiny bit acquainted of this your broadcast offered bright clear idea

  12. I’m gone to inform my little brother, that he should also pay a quick visit this web site on regular basis to obtain updated from most up-to-date gossip.

  13. I was more than happy to discover this website. I wanted to thank you for ones time for this fantastic
    read!! I definitely liked every part of it and i also have you bookmarked to
    look at new information on your website.

  14. Cool blog! Is your theme custom made or did you download it from
    somewhere? A design like yours with a few simple adjustements would really make my blog stand out.
    Please let me know where you got your design.
    With thanks

  1. Pingback: Quora

  2. 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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s


Get every new post delivered to your Inbox.

Join 58 other followers

%d bloggers like this: