I want to search for a parentheis ( using ack, but neither using ( nor \( worked.

The first one ("(") is recognized as a multiple line command. The second one ("\(") is treated as an invalid regular expression.

How can I search for a parenthesis character with ack?

Error message

 % ack foo\(
Invalid regex 'foo(':
  Unmatched ( in regex; marked by <-- HERE in m/foo( <-- HERE / at /usr/local/bin/ack line 332.
  • Are you sure? echo '(' | ack "\(" work fine.
    – cuonglm
    Mar 4 '15 at 9:44
  • @cuonglm: it works for me too Mar 4 '15 at 14:11
  • @ironsand: show exact errors you get and tell us your ack version Mar 4 '15 at 14:11
  • I added the error message. I got error message because I forget to quote the word. (My question was edited and the words are quoted.)
    – ironsand
    Mar 5 '15 at 1:38

To search for a parenthesis character, pass backslash+parenthesis to ack.

Both backslash and parentheses are special in the shell, so you need to quote them when you're entering them in a shell script or on the command line. The simplest form of quoting is with single quotes: this tells the shell to pass everything through literally except single quotes themselves.

ack '\('

You can also use backslashes to protect special characters from shell expansion, but it tends to be less readable:

ack \\\(

The first backslash quotes the second one in the shell, so that the first character of the argument to ack is \. The third backslash quotes the parenthesis, so the second character of the argument to ack is (. Like in the case with single quotes above, the argument that ack sees is the two-character string \(.

ack ( doesn't work because the shell sees an opening parenthesis and treats it as shell syntax. ack \( doesn't work because the shell passes ( as the argument to ack, and ack interprets the parenthesis in the regular expression as indicating the start of a group.


i don't know the exact error you get, but here it works using escaped parenthesis ("\(")

 $ cat > test.c <<EOF
 int main() {
   return 0;
 $ ack '\(' test.c 
 int main() {

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.