How to match a string containing the character "$" with grep?
How to match strings that contain
Example – by the following
grep doesn’t return the match.
param="ambari_parameter$" echo "ambari_parameter$" | grep $param echo "ambari_parameter$" | grep "$param"
In this case, the string in
$param will be interpreted as a regular expression. This expression contains a
$ which anchors the
ambari_parameter in the pattern to the end of the match. Since
ambari_parameter$ in the input data does not contain
ambari_parameter at the very end (due to the
$ at the end of the string), it won’t match.
You could just escape the
$ in the pattern (
\$ in a double quoted string), or place the
$ in a bracketed group as
[$], but since you seem to want to do a string match rather than a regular expression match, it may be more appropriate to use
echo 'ambari_parameter$' | grep -F -e "$param"
grep -F makes
grep treat the given pattern as a string rather than as a regular expression. Each character in the pattern will therefore be match literally, even the
$ at the end.
I also used
-e here to force
grep to recognize the following argument as a pattern. This is necessary if
$param starts with a dash (
-). It’s generally good to use
-e whenever the pattern you match with is given in a variable.
To further require that the whole input line matches the complete string, add
echo 'ambari_parameter$' | grep -xF -e "$param"
ambari_parameter$, then this will not match the string
ambari_parameter$100 (but will match if
-x is left out).
You should escape the special characters in case you are looking for exact match of them as this ways:
if it’s in single quote, one escape is required.
if it’s in double quote or not quoted, one escape more is required.
grep "parameter\$" #or grep parameter\$
Note that any space within match if not quoted, should be escaped as well.
grep para meter\$
If you have a whole pattern you want to match literally then don’t treat it as a regular expression. If you do need to match a literal
$ in a regular expression, use
, and several other punctuation characters,
$ is a metacharacter in regular expressions. The
$ character matches the empty string at the end of a line (so
x but only at the end of a line). If your pattern doesn’t contain any metacharacters, then you don’t need to treat it as a regular expression and you should use
grep -F as Kusalananda explains.
If you really do need to match a
$ or any other metacharacter literally in a regular expression, escape it with the
metacharacter. To match a literal
$, to match a literal
\, and so forth.
param='ambari_parameter$' echo "ambari_parameter$" | grep "$param"
When you assign to
param in the shell, enclose the regular expression in single quotes (
') as shown above, because
also have special meanings in most shells, including all Bourne-style shells (you tagged bash).
in shells works basically the same as in regular expressions. (
$ doesn’t; it’s used for various shell expansions/substitutions, especially parameter expansion).
Unquoted, or even in double quotes,
$ indicates a literal
$ in the shell, which is then passed to
grep interprets as a regular expression metacharacter. Enclosing it in single quotes prevents this. You could alternatively write
\$–or sometimes just
\$–but that’s confusing.