Grep matches exact substring ignoring regex syntax

Is there a way to get grep to match the exact string and not parse it as a regular expression? Or is there any tool for the right line for grep?

$ version=10.4 $ echo "10.4" | grep $version 10.4 $ echo "1034" | grep $version # shouldn't match 1034 
+6
source share
2 answers

Use grep -F or fgrep.

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

See the man page:

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

I searched for the term "literal match" or "fixed string".

(See also Using grep with a complex string and How can grep literally interpret a string containing an asterisk and feed it into grep through a variable? )

+11
source

This should work ... this works for me:

 echo "1034" | grep "10\.4" 
0
source

Source: https://habr.com/ru/post/892298/


All Articles