
You must take both into account, though if you quote your regular expression then you can prevent the shell from treating it specially and ensure that it passes it unchanged to grep. * has a special meaning both as a shell globbing character ("wildcard") and as a regular expression metacharacter. This may not be the desired behavior in case it's not, you can turn on grep's PCRE engine (using the -P option) and append the ? metacharacter, which when put after the * and + metacharacters has the effect of changing their greediness: % cat infileġ: Basic Regular Expressions, Extended Regular Expressions and Perl Compatible Regular Expressions it will match the longest match: % cat infile The * metacharacter in BREs and EREs is always "greedy", i.e. metacharacter, which matches any character: % cat infile

To match 0 or more occurences of any character, you want to match 0 or more occurences of the.


This means that in the This*String pattern, being the * metacharacter not preceded either by a grouped pattern or a character class, the * metacharacter matches 0 or more occurences of the previous character (in this case the s character): % cat infile The * metacharacter in BRE 1s, ERE 1s and PCRE 1s matches 0 or more occurences of the previously grouped pattern (if a grouped pattern is preceding the * metacharacter), 0 or more occurences of the previous character class (if a character class is preceding the * metacharacter) or 0 or more occurences of the previous character (if neither a grouped pattern nor a character class is preceding the * metacharacter)
