17 Comments

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:

#!/bin/bash
# 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
pass=${bldblu}*${txtrst}
warn=${bldred}*${txtrst}
ques=${bldblu}?${txtrst}

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:

#!/bin/bash
# tputcolors

echo
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)"
done

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

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

About these ads

About Todd Partridge (Gently)

Good times, good people, good fun.

17 comments on “Color Output on Bash Scripts

  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
    dollar=”${c_bold_on}\\$”

    Just my 2 copecks :) and just a different approach

  3. damn, forget the main part :-D

    escape()
    {
    echo “\[33[$1\]”
    }

  4. Grim,

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

  5. How do you apply a style to only the command’s line in bash?…

    The canonical way to control your terminal’s settings from a shell script under UNIX/Linux would be to use the tput command to look up the escape sequences which are needed for your terminal. For example you can use something like: bold=$(tput …

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

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

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

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

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

  11. On a 256-color terminal like xterm, PuTTY, or Terminal.app (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!

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

    http://www.cinetelabrasil.com.br/wiki/index.
    php?title=Wrinkle_formula

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

  14. […] via Color Output on Bash Scripts | Linux Tidbits. […]

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

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

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

Follow

Get every new post delivered to your Inbox.

Join 51 other followers

%d bloggers like this: