Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I search for (escape) a dollar sign using ack?

Tags:

regex

unix

perl

ack

I want to ack for the literal string: "$$" in a code base, but escaping the dollar sign like this:

ack \$\$

doesn't work.

like image 783
Christopher Scott Avatar asked Feb 21 '13 20:02

Christopher Scott


1 Answers

You are getting confused by shell quoting. When you type:

ack "\\\$\\\$\("

the shell interpolates the double quoted string so that \\ is translated to \, \$ is translated to $ and \( is translated to \( and ack gets the string \$\$\( as its argument. It is much simpler to avoid the shell interpolation by using single quotes and invoke:

ack '\$\$\('

Replace ack with echo to explore how the shell is expanding the strings. Note that

ack "\\$\\$\("

will also work, but for slightly different reasons. Here, the first two \ are treated as a single (escaped) \, then the $ is translated as a $ because it is followed by a character that is not a valid character in a variable name. \( expands to \( instead of simply ( because ( is not subject to interpolation and therefore does not need to be escaped. But note that outside of double quotes, \( is converted to (.

Shell quoting rules get confusing sometimes!

like image 80
William Pursell Avatar answered Oct 31 '22 17:10

William Pursell