Bash - adding color

From NoskeWiki
Jump to: navigation, search

About

NOTE: This page is a daughter page of: Unix


A shell with only one color text is boring. Here's some methods I've found to add some color to the output via the prompt, output or substituting into existing text. Try each command and if you like it, add it to your ~./bashrc file.


ANSI Colors and Formatting

UNIX has limited colors... they are the ANSI escape code colors shown here:

ANSI Color table
Intensity 0 1 2 3 4 5 6 7
Normal Black Red Green Yellow Blue Magenta Cyan White
Bright Black Red Green Yellow Blue Magenta Cyan White

Another important set of numbers are the ANSI CSI [SGR (Select Graphic Rendition) parameters], which you can think of as font formatting:

ANSI SGR table
Code: 0 1 2 3 4 ...
Formatting: Normal/reset bold faint italic underlined ... and many more here


Printf - using ANSI codes

The fastest way to output color is using and printf. Try this in the command line:

$ printf "My favorite colors are \e[31m red \e[0m , \e[32m green \e[0m and \e[44m blue \e[0m \n"
$ printf "I like my text \e[1m bold \e[0m , \e[3m italic \e[0m , \e[4m underlined \e[0m or \e[7m inverted \e[0m \n"
$ printf "Be \e[31;1m bold & red! \e[0m \n"
$ printf "Be \e[37;4;2m dark gray (dark white) and underlined \e[0m \n"
  • Colors use ANSI intensity values from the first table.
    • Use 30+intensity<code> for foreground, <code>40+i for background color.
  • Formatting uses SGR code values from the second table.
    • Be warned that, bold can alters colors on different terminals.
  • Use \e[0m to revert to default color/formatting.
  • You can combine color and formatting with \e[3<ANSI>;<SGR>m - as in \e[31;1m
    • You can get an altered color with \e[3<ANSI>;2m .
  • There is always an m on the end, but there are several versions of the prefix you may encounters (all do the same thing I believe): \e[, \033[ and \x1b[ <-- this last one is the most versatile.
  • These codes don't work in echo - see "tput" section below.


Change prompt color

To color just your prompt try:

$ PS1='\e[1;34m\u@\h:\e[0;34m\w\e[0m\$ '
anoske:/desktop/path$ 

In this code the "\e[0;34m" (sometimes written "\033[34m") parts specify color, while \u=user, \h=host, w=full path, W=last part of path.


Grep - search word highlighted in color

When you run grep on anything you can make sure the keywords are colored. Sadly you can only chose one color though.

$ echo 'my ERROR color' | grep 'ERROR'

(ERROR will be in red by grep default)

$ export GREP_OPTIONS='--color=auto' GREP_COLOR='1;32'
$ ./myprogramwithoutput | grep --color=auto 'ERROR'

(ERROR is now bold green)


Sed - used to add color through string substitution

The sed (stream editor) command can be used to add ANSI color codes and have different word patterns in different colors by stacking them up.

$ printf 'my ERROR color' | sed s/ERROR/`printf "\033[32mERROR\033[0m"`/
my ERROR color

(ERROR will be in green)

$ echo 'my ERROR color' | sed -e "s/ERROR/\x1b[32mERROR\x1b[0m/g"

(same result as above) ... Be warned you but MUST use the '\x1b' escape character - '\e' and '\033' don't play well with sed.

$ alias sedtestoutput='sed -e "s/Actual:/\x1b[32mActual:\x1b[0m/g" -e "s/Expected value:/\x1b[0;34mExpected value:\x1b[0m/g" -e "s/Failure:/\x1b[0;31mFailure:\x1b[0m/g"'
$ echo 'Look \nActual: val1, Expected value: val2\n Failure: .... \nExpected: hi' | sedtestoutput

(Actual:=green, Expected value:=blue, Failure:=red) ... Notice: unlike printf, echo will just print '\n' as '\n' instead of newlines.


If you have lots of -e rules (like example above) you may like to create a sed script file and input this script with "sed -f script.sed":

rules.sed:

#!/bin/sed -f
s/Actual:/\x1b[32m&\x1b[0m/g
s/Expected value:/\x1b[0;34mExpected val:\x1b[0m/g
s/^Fail.*:/\x1b[0;31m&\x1b[0m/g

Now run:

$ printf 'Look \nActual: val1 \nExpected value: val2 \nFailure: .... \nExpected: hi' | sed -f rules.sed

(Actual:=green, Expected val:=blue, Failure:=red) ... Notice that the '&' repeats the regex match.


Tput - changing command mode for echo

The tput command access terminal capabilities including: changing the background (tab) color, foreground (text) color, and various "modes" like underline, bold and so on - and this will affect your echo output. Your tput command add together until the next command line... for example:

$ tput smul; echo "underlined"; tput setaf 1; echo "underlined & red"
$ echo "back to normal text"

The commands options are:

Command Action
tput setaf color Set text color
tput smul

tput rmul

Start underline mode

Exit underline mode

tput bold Set bold mode
tput setab color Set background color
tput sgr0 Reset all attributes

... and some less useful ones: tput dim (half-bright mode) && tput rev (reverse mode) && tput smso (set standout mode) && tput rmso (exit standout mode).

The available colors are:

Color Code
Black 0
Red 1
Green 2
Yellow 3
Blue 4
Magenta 5
Cyan 6
White 7

... same as ANSI intensity values.


See Also

Links