I want to ack for the literal string: "$$
" in a code base, but escaping the dollar sign like this:
ack \$\$
doesn't work.
I want to ack for the literal string: "$$
" in a code base, but escaping the dollar sign like this:
ack \$\$
doesn't work.
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!
You can escape the dollar sign character with three backslashes, like this:
ack "\\\$\\\$"
or use single quotes, where you only have to escape it once:
ack '\$\$'
ack '\$\$\('
should work just fine. –
Abbey You can use printf
to take care of the quoting for you by using the %q
format specifier.
$ printf %q '$$('
\$\$\(
help print
has the following to say (I'm assuming bash here)`
%q quote the argument in a way that can be reused as shell input
© 2022 - 2024 — McMap. All rights reserved.
ack '[$][$]'
. Character class. Cleaner than N escapes. – Carver