1
[USERNAME@host ~] echo -e 'prdxxx\ndadxxx' | grep "da*xxx"
prdxxx
dadxxx
[USERNAME@host ~] echo $SHELL
/bin/bash
[USERNAME@host ~] dpkg -l | grep -iw bash
ii  bash                                    4.1-2ubuntu3                                    The GNU Bourne Again SHell
ii  bash-completion                         1:1.1-3ubuntu2                                  programmable completion for the bash shell
[USERNAME@host ~] 

Why does da*xxx find prdxxx too? It doesn't contains da... did I found a grep bug? or is this a feature?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
gasko peter
  • 5,434
  • 22
  • 83
  • 145
  • 2
    Related to your confusion, see [Why do regular expressions differ from that used to filter files](http://unix.stackexchange.com/questions/57957/why-do-regular-expressions-differ-from-that-used-to-filter-files). – manatwork Jan 02 '13 at 11:48

2 Answers2

10

It is working fine as per the meaning of the '*'.

* -> 0 or more occurences of prev character.

Since you are checking for a*, this will match 0 or more a's. This means da*xxx can match dxxx, daxxx, daaxxx, daaaxxx, and so on.

Guru
  • 5,855
  • 1
  • 19
  • 20
4

There is a difference between normal shell file name patterns (called glob) where * matches any number of unknown characters, and regular expressions that are used for example by grep, where * stands for zero or more occurences of the previous pattern (this is the character a in your example).

jofel
  • 26,513
  • 6
  • 65
  • 92