0

The issue is described as follows.

# cat test
hello
fedora

# cat test | grep h*o
hello

# cat test | grep 'h*o'
hello
fedora

# cat test | grep "h*o"
hello
fedora

grep h*o works as expected, but WHY do grep 'h*o' and grep "h*o" get fedora which contains no letter h?

xmllmx
  • 39,765
  • 26
  • 162
  • 323
  • 1
    grep speaks regex, not glob-style patterns. `*` means a different thing in the different languages. – Charles Duffy Jul 06 '16 at 02:19
  • 4
    All 3 produce the same results for me. `*` means *any number of `h`* (including none) followed by an `o`. So your examples in quotes make sense. However, your first case with no quotes will cause the shell to match `h*o` to whatever is in your current directory as a file name and use that as the string to match. What other file names are in your current directory? I don't have a file in my current directory matching `h*o`, so it works fine. – lurker Jul 06 '16 at 02:19
  • 3
    Do you have a file named `hello` in the same directory? If so, part of the answer to this particular question is that, in your second example, `h*o` is getting expanded to `hello` by your shell *before* getting passed to `grep`, so `fedora` is not matched. – leekaiinthesky Jul 06 '16 at 02:22
  • 1
    @leekaiinthesky, yes, i have a file named hello in the current directory. – xmllmx Jul 06 '16 at 02:27
  • 1
    To do what you (appear to) mean, you should use a quoted string so the shell doesn't expand it, and `.*` to match anything. So `cat test | grep "h.*o"` – mykel Jul 06 '16 at 02:30

0 Answers0