How to match a string containing the character "$" with grep?

How to match strings that contain $ character?

Example – by the following grep doesn’t return the match.

  echo "ambari_parameter$" | grep $param
  echo "ambari_parameter$" | grep "$param"
Asked By: yael


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 $ as $ 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 -F:

echo 'ambari_parameter$' | grep -F -e "$param"

Using 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 -x:

echo 'ambari_parameter$' | grep -xF -e "$param"

If $param is ambari_parameter$, then this will not match the string _ambari_parameter$ or ambari_parameter$100 (but will match if -x is left out).

Answered By: Kusalananda

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.

grep 'parameter$'

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\$
Answered By: αғsнιη

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 $.

Like ., , 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$ matches 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 $ write $, to match a literal write \, and so forth.

echo "ambari_parameter$" | grep "$param"

When you assign to param in the shell, enclose the regular expression in single quotes (' ') as shown above, because $ and also have special meanings in most shells, including all Bourne-style shells (you tagged ). 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 as $ which 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.

Answered By: Eliah Kagan
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.