Shell – grep ignoring pipe when aliased to ‘grep -R’

aliasgreppipeshell

I'm trying to pipe the output of a grep search into the input of another grep.
Such as:

grep search_query * | grep -v but_not_this

But the second grep is not using the output of the previous search. It looks like the second grep is just using * instead. For example,

grep lcov *                                                                                          
tst/bits/Module.mk:21:$(call func_report_lcov)
tst/drivers/Module.mk:27:$(call func_report_lcov)

But when I want to filter out the results containing "call",

grep lcov * | grep -v call
...

Grep gives me every single line in my workspace that doesn't contain "call".

Environment Info:

  • This is happening in both bash and fish
  • I have aliased the grep command like so alias grep='grep -nR --color=always'

Anything else I might be missing?

Best Answer

The alias is what is causing it. From man grep, the -R option causes grep to "read all files under each directory, recursively". Hence, the part after the pipe ignores the output from the first grep, and instead greps through all files recursively from the current directory.

You can bypass the alias and use vanilla grep with \grep. Hence the following should give you what you expect.

grep lcov * | \grep -v call

However, I personally think that putting -R in the alias is confusing.

Related Question