Grep: a better way to temporarily disable colors?

i was just using gnu grep

grep (GNU grep) 2.20

and when running a command i kept getting really weird results, namely that eval and echo and various combinations would not run the command

BIN="${FFMPEG} -i ${INPUT} ${setX265} ${videoCrop} ${audioOptions} ${OUTPUT}.mkv"

with this "bad" result

crop=304:208:6:8 

and when i dumped the result to a file, it actually had this

^[[m^[[K^[[01;31m^[[Kcrop=304:208:6:8^[[m^[[K^[[m^[[K 

finally it came down to my .bashrc file where i had defined

export GREP_OPTIONS='-irs --color=always --line-number --exclude-dir=.git'

in my script file i used this to temporarily disable grep colors, etc

unset GREP_OPTIONS

so it there a better way to turn off the grep options than having to switch this on/off?

You can define shell variables temporarily for just the next command:

GREP_OPTIONS= command
1 Like

Maybe you should ask a different question. Why are you defining options to be applied to every invocation of grep you make in your .bashrc file if you don't want those options to be applied every time you invoke grep ?

Have you considered defining an alias for grep that includes the options you want to apply only when grep is invoked interactively?

1 Like

Because when i grep from the command line i don't want to have to type in this every time.

'-irs --color=always --line-number --exclude-dir=.git'

if i did an alias, it could easily be stomped on by another shell or script running or if i used a different grep like BSD, it would probably choke on an alias.

i'm really looking for something simple that would just strip out the '^[K' formatting characters like with

 echo

or

printf

something that recognizes these as not to print or is so dumb that it ignores anything but the displayed string without attributes so that when i pipe something to my script file i don't have turn off or unset switches

You can't have it both ways. If you want to make EVERY invocation of grep use the flags you are setting in GREP_OPTIONS , you are all set. But, as you have seen, that breaks scripts that depend on grep to do what the standards say grep is supposed to do.

If you want interactive invocations of grep to use a specific set of options, use an alias . If you want interactive invocations of grep to use a specific set of options if, and only if, you are running on a Linux system, define an alias for grep in your .bashrc if, and only if, uname indicates that you're starting bash on a Linux system.

Otherwise, I think you have two options:

  1. rewrite every shell script you use (provided by you or anyone else) that uses grep ,

OR

  1. get rid of the definition of GREP_OPTIONS in your .bashrc and add a new utility ( mygrep ) in your bin directory ( $HOME/bin/mygrep ) that contains something like:
#!/bin/bash
grep -irs --color=always --line-number --exclude-dir=.git "$@"

that you install on Linux systems and on non-Linux systems install a symlink $HOME/bin/mygrep that points to wherever grep is located on that system and learn to use mygrep instead of grep whenever you use it interactively.

Looks like a bug in grep.
It should never add terminal color sequences if output is directed to a file or to a pipe.

Not even when the option-argument is always (as in --color=always )? I don't have a GNU grep to test but I haven't seen where the GNU man page states any exceptions to always in the above case. (Maybe the "on the terminal" in that section of the description means that --color is ignored when output isn't directed to a terminal device, but it doesn't clearly state that behavior.)

I have tested different versions of GNU grep.
Don, you are right, the --color=always puts some default color codes even if the output is not a terminal.
Correct is --color=auto .
Therefore, the .bashrc/.profile should have the environment

export GREP_OPTIONS='-irs --color=auto --line-number --exclude-dir=.git'

that is still inherited by scripts,
or the alias

alias grep='grep -irs --color=auto --line-number --exclude-dir=.git'

that is not inherited by scripts (recommended).
It's a pity that every shell script should have

unset GREP_OPTIONS

for safety.
GNU is not Unix, and suffers from creeping featurism:mad:

2 Likes