Is GREP_OPTIONS= --color=always ignored?

Richard

I was playing around with setting GREP_OPTIONS and couldn't get it to work. Just curious as to why.

Simple test commands output:

richard@ubuntu:~$ echo $GREP_OPTIONS

richard@ubuntu:~$ 

richard@ubuntu:~$ ls | grep o 
Desktop
downloads
Dropbox
ebooks
workspace

richard@ubuntu:~$ ls | grep o --color=always | less -R 

richard@ubuntu:~$ export GREP_OPTIONS="--color=always" 
richard@ubuntu:~$ ls | grep o  | less -R 
  1. The 1st grep outputs to the terminal in color (each 'o' is red)
  2. The 2nd grep outputs via less in color (same as 1)
  3. But the 3rd grep outputs to less but in black & white – but this should be in color.

So it appears that grep is ignoring GREP_OPTIONS. Is that a bug or am I doing something wrong?

(Ubuntu 12.04.2, GNU grep 2.10)

Gilles 'SO- stop being evil'

If grep o produces color output, then either grep is an alias to grep --color=auto or grep --color=always (or possibly more options), or GREP_OPTIONS is set to a value that contains --color=auto or --color=always. Since $GREP_OPTIONS is empty, it must be the alias.

Since grep o | less -R doesn't show colors, the alias must be to grep --color=auto (a sensible choice). With the alias, the grep command always receives the --color option on the command line, and this takes precedence over the environment variable.

If you want to use the environment variable, remove the alias definition from your ~/.bashrc, or for one session run unalias grep. You can replace alias grep='grep --color=auto' by export GREP_OPTIONS='--color=auto': they have essentially the same meaning, except that:

  • setting GREP_OPTIONS to a different value only overrides the latter;
  • the alias only kicks in when you run grep from an interactive shell, whereas setting GREP_OPTIONS also applies when grep is run from scripts and other applications.

Never put --color=always or most other options in GREP_OPTIONS: it would break many programs that parse the output of grep. --color=auto is about the only safe option to put in GREP_OPTIONS. For anything else, use the alias. Future versions of GNU grep will drop support for the option for this reason.

Note that the alias definition goes into ~/.bashrc (it's a shell setting), whereas the environment variable definition goes into ~/.profile (it's a session setting). See Is there a ".bashrc" equivalent file read by all shells?

If you want to run the unaliased command just once, run \grep instead of grep (quoting any part of the name bypasses the alias lookup).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

textColorHint is ignored as hint text color is always colorAccent

From Dev

showAsAction = "always" is ignored in Toolbar

From Dev

Does grep --color default to =auto or =always?

From Dev

CSS color attribute ignored

From Dev

Export multiple options in $GREP_OPTIONS

From Dev

Optional capture always ignored in favor of *

From Dev

Globbing filetypes in bashrc for GREP_OPTIONS --exclude

From Dev

Different results in grep results when using --color=always option

From Java

Android 4.3 menu item showAsAction="always" ignored

From Dev

grep: Ignoring GREP_OPTIONS to search case-sensitive

From Dev

Android layout editor background color gets ignored

From Dev

Why are some setxkbmap options ignored?

From Dev

Why are some setxkbmap options ignored?

From Dev

autoinstall options ignored on Ubuntu 20.04

From Dev

Git Grep color options explained and/or compared

From Dev

Web API 2 OData = $format not working: the request is always ignored

From Dev

Android MenuItem showAsAction="always" Ignored When Have a Group

From Dev

UIView Background Color Always Black

From Java

CardView background color always white

From Dev

TextView color is always white for AppCompatActivity

From Dev

how to always use rgrep with color

From Dev

how to always use rgrep with color

From Dev

Foreground color always takes the white

From Dev

Vim color scheme not always working

From Dev

How to get rid of `grep: warning: GREP_OPTIONS is deprecated; please use an alias or script`?

From Dev

iOS 10 Custom cell color alpha being ignored

From Dev

R shiny gvisColumnChart passed options being ignored

From Dev

jQuery datepicker: options ignored selecting the text field

From Dev

datepicker-options being ignored for bootstrap datepicker

Related Related

  1. 1

    textColorHint is ignored as hint text color is always colorAccent

  2. 2

    showAsAction = "always" is ignored in Toolbar

  3. 3

    Does grep --color default to =auto or =always?

  4. 4

    CSS color attribute ignored

  5. 5

    Export multiple options in $GREP_OPTIONS

  6. 6

    Optional capture always ignored in favor of *

  7. 7

    Globbing filetypes in bashrc for GREP_OPTIONS --exclude

  8. 8

    Different results in grep results when using --color=always option

  9. 9

    Android 4.3 menu item showAsAction="always" ignored

  10. 10

    grep: Ignoring GREP_OPTIONS to search case-sensitive

  11. 11

    Android layout editor background color gets ignored

  12. 12

    Why are some setxkbmap options ignored?

  13. 13

    Why are some setxkbmap options ignored?

  14. 14

    autoinstall options ignored on Ubuntu 20.04

  15. 15

    Git Grep color options explained and/or compared

  16. 16

    Web API 2 OData = $format not working: the request is always ignored

  17. 17

    Android MenuItem showAsAction="always" Ignored When Have a Group

  18. 18

    UIView Background Color Always Black

  19. 19

    CardView background color always white

  20. 20

    TextView color is always white for AppCompatActivity

  21. 21

    how to always use rgrep with color

  22. 22

    how to always use rgrep with color

  23. 23

    Foreground color always takes the white

  24. 24

    Vim color scheme not always working

  25. 25

    How to get rid of `grep: warning: GREP_OPTIONS is deprecated; please use an alias or script`?

  26. 26

    iOS 10 Custom cell color alpha being ignored

  27. 27

    R shiny gvisColumnChart passed options being ignored

  28. 28

    jQuery datepicker: options ignored selecting the text field

  29. 29

    datepicker-options being ignored for bootstrap datepicker

HotTag

Archive