i have a script that is trying to find a line in a text file. It could be completely alphanumerical, but it’s also possible that it contains special characters, mostly < and > as it’s sometimes xml-tags with content.
Basically this is what i am trying to do:
$ grep '<Tag>20240605074212800000001' filename | less
<Tag>Detlef</Tag>
[user@servername /path]
$ STRANGEBEHAVIOR="'<Tag>Detlef'"
[user@servername /path]
$ echo $STRANGEBEHAVIOR
'<Tag>Detlef'
[user@servername /path]
$ grep $STRANGEBEHAVIOR filename | less
(END)
[user@servername /path]
$ grep "$STRANGEBEHAVIOR" filename | less
(END)
[user@servername /path]
$ echo "grep $STRANGEBEHAVIOR filename | less"
grep '<Tag>Detlef' filename | less
I had found out that using grep the next thing works, but using zgrep it does not:
[user@servername /path]
$ STRANGEBEHAVIOR="<Tag>Detlef"
[user@servername /path]
$ echo $STRANGEBEHAVIOR
<Tag>Detlef
[user@servername /path]
$ grep "$STRANGEBEHAVIOR" filename | less
<Tag>Detlef</Tag>
For reasons i do not understand, i can’t find an easy solution. if i manually type my search with single quotes, grep works the way i understand it to, it looks for this string and ignores all special charakters. If i give grep the same information using a variable, grep does not find a match, probably because it is interpreting the single quotes as part of the string, but why?