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).
nice tip :)
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
damn, forget the main part :-D
escape()
{
echo “\[33[$1\]”
}
Grim,
Don’t you have to use “echo -n” to suppress the newline?
Pingback: Quora
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.
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.
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.
Great to know that we can output colors in bash. Thanks!
Reblogged this on Digitally Conscious.
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!
Pingback: Color Output on Bash Scripts | Linux Tidbits | Das OSX Log