How to make grep consider any character regular

I am trying to grep a line in a script. I don't know what line I am finding or what is in the file where I am trying to find it. I just want the grep exact line from the file.

So my problem is that sometimes the greping line contains square brackets and as I found out grep respects their special characters.

string='some [text]'
grep "$string" file

      

I can avoid them with sed

string='some [text]'
grep "$(sed -e 's/\[/\\[/g' <<< "$string")" file

      

I need grep to match the exact line no matter what the input might be. So is there a better way to do this? Is there some way to tell grep to treat every character in the string as a regular character? If not, are there any other special characters like [do I need to worry?

+3


source to share


1 answer


You can use an option -F

to interpret it as a fixed string:



grep -F "$string" file

      

+5


source







All Articles