l'hiverviendra
l'hiverviendra

Reputation: 73

What's the difference between grep \\$ and grep \\\$?

When I type grep \\\$ shell escapes both \ and $ and transforms it to \$ and sends to grep, which then finds all the lines with dollar sign $. That's fine!

When I type grep \\$ the result is the same and I don't really know why. The first backslash should escape the second one, but then $ is not escaped and shell should replace it with an empty string? grep should receive \ and report an error but instead everything works as in first example for some reason..

Upvotes: 2

Views: 247

Answers (1)

CB Bailey
CB Bailey

Reputation: 792427

In UNIX shells, $x is replaced by the value of the shell variable x but when there is nothing following the $, no substitution is performed. You can test this with echo:

> echo $
$
> echo $x

>

Your two grep arguments are passed into grep as exactly the same regular expression.

> echo \\\$
\$
> echo \\$
\$
>

Upvotes: 2

Related Questions