Color For Bash Scripts

Sometimes we want a little color in our lives. Our BASH scripts might seem a little dull with white text on a black background. And, we can get tired of adding color manually to each script we make. So, I made a BASH color library to enhance scripts with colored text and colored text backgrounds. Not only is color fun, it enhances a script’s output and helps convey messages to the user in a colorful way. Booyah! Let’s move on to the color library. In this paragraph, I will explain how to use this BASH color library. First, download the library and test script files and make them executable. After that, we need to source the library in our script by using the “dot” shorthand for the source keyword. This line belongs at the top of your script. In other words, at the top of your script (but after the shebang of course) type in:
. /path/to/the/library/libcolor.bash You can also put the libcolor.bash in a location that your $PATH already knows to make it easy to access the library. However, in my case I put the full path to the library, because of OCD. LoL.
. $HOME/bin/library.bash/libcolor.bash This is how I use the library in my scripts. I have a folder called library.bash that contains all my BASH libraries.
That is all you need to do to add the bash color library to your own scripts. Now, I will discuss how to use the library.

How to Use

There are a two main ways to use the library:
  1. Directly adding the color to your echo statements
  2. Using function calls
For the first method, we would do something like this:
echo -e "${blue}This is blue text.${resetall}"
echo -e "${red}${underline}Red underlined text.${resetall}"
For this method we use our own echo -e commands. Please see the available colors in the library file. For the second method we use less cumbersome syntax and a little more elegance. There are three functions that can be used:
_colortext16 _colorbackground16 _colorfgbg256 The number at the end indicates the total number of possible color combinations. 16 means 16 colors. 256 means 256 fg/bg color combinations.
And, we can use them like this:
_colortext16 red "This is red text."
_colorbackground16 blue "This is a blue background."
_colorfgbg256 yellow white "This is yellow text with a white background."
See the library header and the testcolorlibrary.sh script for more examples. In conclusion, did I mention to see the library header and testcolorlibrary.sh script to see more examples? Oh, yes I did. Phew.
Output from the testcolorlibrary.sh script
Finally, you can change the name of the functions to what ever you want to use as function calls. There is a function called _colortable that can be run from the test script that will show you the different colors (and their code #) that are available. You can then add these to the _tputcolors function to get more color choices.

Downloads

Download the bash color library and use the test script to make sure the library works. For questions or comments you can email blogATgreengumdropsDOTnet.