GREP_COLORS – change the colors in the GREP output.

Today we will look at the variable GREP_COLORS. This variable determines the colour that is used with the grep command. You can look at the man page of the grep command to see what the various options mean. Here is the excerpt from the man command:

    GREP_COLORS
              Specifies the colors and other attributes used to highlight various  parts  of  the
              output.   Its  value  is  a  colon-separated  list of capabilities that defaults to
              ms=01;31:mc=01;31:sl=:cx=:fn=35:ln=32:bn=32:se=36  with  the  rv  and  ne   boolean
              capabilities omitted (i.e., false).  Supported capabilities are as follows.

              sl=    SGR  substring  for  whole  selected lines (i.e., matching lines when the -v
                     command-line option is omitted, or non-matching lines when -v is specified).
                     If however the boolean rv capability and the -v command-line option are both
                     specified, it applies to context matching lines  instead.   The  default  is
                     empty (i.e., the terminal's default color pair).

              cx=    SGR  substring for whole context lines (i.e., non-matching lines when the -v
                     command-line option is omitted, or matching lines when -v is specified).  If
                     however  the  boolean  rv capability and the -v command-line option are both
                     specified, it applies to selected non-matching lines instead.   The  default
                     is empty (i.e., the terminal's default color pair).

              rv     Boolean  value  that  reverses  (swaps)  the  meanings  of  the  sl= and cx=
                     capabilities when the -v command-line option is specified.  The  default  is
                     false (i.e., the capability is omitted).

              mt=01;31
                     SGR  substring  for  matching  non-empty  text in any matching line (i.e., a
                     selected line when the -v command-line option is omitted, or a context  line
                     when  -v  is specified).  Setting this is equivalent to setting both ms= and
                     mc= at once to the same value.  The default is a bold  red  text  foreground
                     over the current line background.

              ms=01;31
                     SGR substring for matching non-empty text in a selected line.  (This is only
                     used when the -v command-line option is omitted.)  The effect of the sl= (or
                     cx=  if  rv) capability remains active when this kicks in.  The default is a
                     bold red text foreground over the current line background.

              mc=01;31
                     SGR substring for matching non-empty text in a context line.  (This is  only
                     used  when  the -v command-line option is specified.)  The effect of the cx=
                     (or sl= if rv) capability remains active when this kicks in.  The default is
                     a bold red text foreground over the current line background.

              fn=35  SGR  substring  for file names prefixing any content line.  The default is a
                     magenta text foreground over the terminal's default background.

              ln=32  SGR substring for line numbers prefixing any content line.  The default is a
                     green text foreground over the terminal's default background.

              bn=32  SGR substring for byte offsets prefixing any content line.  The default is a
                     green text foreground over the terminal's default background.

              se=36  SGR substring for separators that are inserted between selected line  fields
                     (:),  between context line fields, (-), and between groups of adjacent lines
                     when nonzero context  is  specified  (--).   The  default  is  a  cyan  text
                     foreground over the terminal's default background.

              ne     Boolean  value that prevents clearing to the end of line using Erase in Line
                     (EL) to Right (\33[K) each time a colorized item ends.  This  is  needed  on
                     terminals on which EL is not supported.  It is otherwise useful on terminals
                     for which the back_color_erase (bce) boolean terminfo  capability  does  not
                     apply,  when  the  chosen  highlight colors do not affect the background, or
                     when EL is too slow or causes too much flicker.  The default is false (i.e.,
                     the capability is omitted).

Now time for some examples:

1) With GREP_OPTIONS set to “”

1
export GREP_OPTIONS=

 

grep color options
grep color options

2) with the following set:

1
export GREP_COLORS="fn=34:mc=01;30:ms=33:sl=21:cx=31"

 

grep color options
grep color options

3) and just the colour for filename and match:

1
export GREP_COLORS="mc=00;36:ms=31:mt=01;38"

 

grep color options
grep color options

4) Slightly lighter colour:

1
export GREP_COLORS="mc=00;36:ms=31:mt=01;33"

 

grep color options
grep color options

5) Default colour for line numbers:

 

grep color options
grep color options

6) Line number colour:

1
export GREP_COLORS="mc=00;36:ms=31:mt=01;38:ln=31"

 

grep color options
grep color options

Hope you will put some colour to grep.

 

22/Apr/2019 : Edit – There is one more very useful article that you should check out – https://www.jenreviews.com/color-meaning/

Enhanced by Zemanta

gnome-terminal color schemes.

Colours
Colours (Photo credit: CherrySoda!)

I was looking for a way to set the terminal colors for gnome. There is a GUI way to change the same by going to the Preferences->Colors. And then you can change the colors that you see in the color pallete in the bottom of the dialog box.

But I was looking at doing it faster and better, and finally I found this page.

http://www.sharms.org/blog/2012/08/gnome-terminal-color-schemes/

Quite interesting and good.

Enhanced by Zemanta

colors in bash scripts

Colours
Colours (Photo credit: CherrySoda!)

I have been trying to understand the color codes for bash for a pretty long time, but somehow never got time to understand this clearly. So this time around when I was writing a script to analyze some logs. I thought I will give it a go and finally understood some part of this.

So, first we will start with this script. This is taken from here.

#!/bin/bash -
#===============================================================================
#
#          FILE:  colortest.sh
#
#         USAGE:  ./colortest.sh
#
#   DESCRIPTION:   http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html
#
#       OPTIONS:  ---
#  REQUIREMENTS:  ---
#          BUGS:  ---
#         NOTES:  ---
#        AUTHOR: Amit Agarwal (aka), amit.agarwal@roamware.com
#       COMPANY:
#       CREATED: 09/04/12 14:29:47 IST
# Last modified: Sun Apr 15, 2012  06:29PM
#      REVISION:  ---
#===============================================================================

#!/bin/bash
#
#   This file echoes a bunch of color codes to the
#   terminal to demonstrate what's available.  Each
#   line is the color code of one forground color,
#   out of 17 (default + 16 escapes), followed by a
#   test use of that color on all nine background
#   colors (default + 8 escapes).
#

T='gYw'   # The test text

echo -e "\n                 40m     41m     42m     43m\
     44m     45m     46m     47m";

for FGs in '    m' '   1m' '  30m' '1;30m' '  31m' '1;31m' '  32m' \
           '1;32m' '  33m' '1;33m' '  34m' '1;34m' '  35m' '1;35m' \
           '  36m' '1;36m' '  37m' '1;37m';
  do FG=${FGs// /}
  echo -en " $FGs \033[$FG  $T  "
  for BG in 40m 41m 42m 43m 44m 45m 46m 47m;
    #do echo -en "$EINS \033[$FG\033[$BG  \$T  \033[0m";
    do echo -en " \033[$FG\033[$BG  $T  \033[0m";
  done
  echo;
done
echo

This script will give you all the colors that are possible in the bash scripting. And now let’s understand color’s a little more.

There are basically two ways you can use colors and I will illustrate both with echo command.

Before we get into all these, we will see hot to reset the colors:

echo -e "\033[00m"

The first one is the one used in the above script, where in you can use the codes multiple times to get the desired effect. For example, see the screenshot below:

colors in bash
colors in bash

And the other way to achieve the same is to use all the color codes with one escape sequence :

colors in bash
colors in bash

Important to note here is that the “m” is missing.

All the codes that you want can be used separated by “;” and end with “m”. I think I have explained the things.

Enhanced by Zemanta