What does grep do when using a dollar sign in the search term?

3

I wanted to find $featuredicon variable in my php files, and I ran grep -ir "$featuredicon"

I understood that dollar sign is reserved character in shell. But what does it actually do? Did I modify my php files? I'm afraid I did something bad...

next time I will run grep -ir "\$featuredicon"

user98645

Posted 2012-07-23T19:03:25.690

Reputation:

Welcome to Super User. Remember to accept the answer that actually answers your question, which is not necessarily the first answer you receive. – Michael Hampton – 2012-07-23T20:31:15.253

Answers

4

The reason why every single line of every single file was listed was due to variable substitution of your shell.

When you call

grep -ir "$featuredicon" *

bash will evaluate that. It will look up the variable $featuredicon and put it into your command. Guess what $featuredicon most likely is?

Right, nothing. So what you're actually doing is:

grep -ir "" *

And that matches every line of every file.

Old Answer

The dollar sign ($) is a placeholder for end-of-line in regular expression (I assume the same is true for grep).

If you want to search for a dollar sign, use '\$'.

Be sure to check the StackOverflow question: How to grep for the dollar symbol ($)?

Der Hochstapler

Posted 2012-07-23T19:03:25.690

Reputation: 77 228

Ty, escaping part I figured it out myself. – None – 2012-07-23T20:29:38.843

I was just curious what it did to my php files, or why it was outputing everything to terminal. – None – 2012-07-23T20:30:09.623

@SandroDzneladze: I updated my answer to address your specific concerns. I was misinterpreting your issue. – Der Hochstapler – 2012-07-23T21:37:12.337

1

grep cannot modify files on its own; it has caused no damage.

Ignacio Vazquez-Abrams

Posted 2012-07-23T19:03:25.690

Reputation: 100 516

1But what does it do? why did it print all lines from every single file? And how to use $ in grep for what purpose? ty for putting my ocd mind to ease :) – None – 2012-07-23T19:13:14.350

1This is not an answer and I feel stupid telling you this :P – Der Hochstapler – 2012-07-23T19:16:39.120

1@Oliver: "Did I modify my php files?" – Ignacio Vazquez-Abrams – 2012-07-23T19:17:11.247

@IgnacioVazquez-Abrams: I guess, you're right. Sorry for judging too quickly. – Der Hochstapler – 2012-07-23T19:27:25.707