HorusKol
HorusKol

Reputation: 8706

command-line grep for a string with '$' in it?

I'm trying to find where two variables are being concatenated in a directory of scripts, but when I try the following:

grep -lire "$DATA_PATH . $AWARDS_YEAR" *

I get "undefined variable" errors...

I thought I could escape the $s by using:

grep -lire "\$DATA_PATH . \$AWARDS_YEAR" *

But I get the same error - so, how do you grep for strings with $s in?

Upvotes: 1

Views: 1110

Answers (2)

Michael Haren
Michael Haren

Reputation: 108296

Put it in single quotes, with the escaping slash:

grep -lire '\$DATA_PATH . \$AWARDS_YEAR' *

Also note, that the dot (.) is a regex character. If you don't want it to be, escape it, too (or don't use the -e option).

Here's a nice reference with more general info.

Upvotes: 2

DigitalRoss
DigitalRoss

Reputation: 146093

Tcsh is a little different about variables than the usual shells, and it's the default on FreeBSD.

So, just use single quotes, '$VAR', or escape the $ outside of the quotes: \$"VAR"

Upvotes: 7

Related Questions