Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Possible Duplicate:
In a regular expression, which characters need escaping?

I know there is the -E flag which treats the "search term" as a regular expression. However, it seems that even without that, grep is treating some characters specially.

For example, if I run:

grep "$var" *

Then it lists every line of the file (I assume it's using $ as the end-of-line regex). The period also seems to look for any character under certain circumstances.

So what are the special characters I need to be wary of when using grep?

share|improve this question
add comment

marked as duplicate by Michael Mrozek Sep 21 '11 at 23:28

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

By default grep uses Basic Regular Expressions (BRE's). Take a look at any resource on BRE's (e.g. man grep and man regex) to learn the syntax. The $ in "$var" is not being passed to grep at all because it is special to the shell. If you have a shell variable named var its expansion is what grep actually sees. If you don't, then grep just gets passed an empty string which matches everywhere.

EDIT: From the POSIX specification for BRE's, the special characters are *, ^, $, ., [, \

If you just want to match literal strings without having to worry about which characters have special meaning in BRE's, you should probably be using fgrep / grep -F instead (they are the same thing; both match fixed strings).

share|improve this answer
add comment

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