14

Is there some way to make grep match an exact string, and not parse it as a regex? Or is there some tool to escape a string properly for grep?

$ version=10.4
$ echo "10.4" | grep $version
10.4
$ echo "1034" | grep $version # shouldn't match
1034
johv
  • 4,424
  • 3
  • 26
  • 42

1 Answers1

24

Use grep -F or fgrep.

$ echo "1034" | grep -F $version # shouldn't match
$ echo "10.4" | grep -F $version
10.4

See man page:

   -F, --fixed-strings
         Interpret PATTERN as a list of fixed strings, separated
         by newlines, any of which is to be matched.

I was looking for the term "literal match" or "fixed string".

(See also Using grep with a complex string and How can grep interpret literally a string that contains an asterisk and is fed to grep through a variable?)

Community
  • 1
  • 1
johv
  • 4,424
  • 3
  • 26
  • 42