3

I'm searching for lines with $ and consecutively ' or " through this grep in my terminal:

grep "\\$('|\")" -rin folder_path

After pressing enter the terminal doesn't think the line is complete so it adds another line for me to keep writing. If I press Tab while writing folder_path it shows this error:

bash: command substitution: line 107: syntax error: file premature end
bash: command substitution: line 106: unexpected file premature end while searching for `''

Those error messages were translated because a part of them are shown in portuguese. I'm using Ubuntu 13.10.

What is wrong?

2 Answers 2

3

Use this:

grep -rn "\$\('\|\"\)" /path/to/directory

Your problem is actually two-fold :

  • From grep's perspective, You are using the Extended Regex syntaces i.e. (, |, ) without escaping them (to treat them special) inside your Basic Regex pattern. To overcome this either escape all those to treat them special or use the -E option of grep.

  • Another problem caused by the above not escaping issue is that $() is the bash command substitution pattern, as you have not escaped the relevant characters bash was treating the pattern $() as command substitution. Note that the \\$() actually makes the command \ plus command substitution as first \ will make the second \ literal and the remaining command substiturion pattern will be left as it is.

Also note that -i is needless here as we are not searching alphabatic characters. You might be interested in printing the file names :

grep -Hrn "\$\('\|\"\)" /path/to/directory

Another thing is that you could get away without escaping $ (indicates the end of line) in this case becasue there are characters to match after $, as a result grep will treat it literally :

grep -Hrn "$\('\|\"\)" /path/to/directory
0
1

No idea what you're doing wrong, as I am not so good with grep, but you should be doing this grep "\$'\|\$\"", basically search for $' or $", and notice that those expressions are all escaped

Here's an example:

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .