I'm using the following code

$ rpm -qa | grep -i gcc

$ rpm -qa | grep -i *gcc*

I would expect the second way to do the same as the first, but it prints nothing. Am I using the grep command correctly?


One way would be to use egrep, or grep -E like this:

rpm -qa | egrep -i '.*gcc.*'

This uses the supplied search string as a regular expression where the period (.) stands for any character and the asterisk (*) stands for one-or-more of those characters.

What I believe you're trying to use with rpm -qa | grep -i *gcc* is a feature of most shells called filename globbing. This technique doesn't work with grep. It assumes that you want everything with gcc in the string, as in your first example, without the need to specify it explicitly.

Edit: The difference between *gcc* and .*gcc.* is the first is actually a syntax error with egrep, though it may not tell you. The asterisk (*) is a modifier that tells the regular expression parser that you're interested in zero-or-more of the previous character. In the first expression, there is no previous character because the single quote (') doesn't count.

  • Just updated my question. Could you also explain the second part. I couldnt put the second part down as a comment because of formatting issues – Rajeshwar Oct 22 '14 at 0:00
  • 2
    I don't think the OP is trying to use filename globbing -- that's the problem. The answer is: always quote regular expressions, so the command should have been grep -i "*gcc*" -- except that's wrong, too. grep -i ".*gcc.*" would be syntactically correct, but it means the same as grep -i "gcc". – G-Man Says 'Reinstate Monica' Oct 22 '14 at 0:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.