Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

When I write

$ grep \$  

then whatever I type on terminal, is matched and printed on terminal. How is \$ being interpreted?

share|improve this question

migrated from stackoverflow.com Apr 16 '10 at 1:25

This question came from our site for professional and enthusiast programmers.

5 Answers 5

The shell is interpreting the \$ and passing it through to grep as $, the end of line character. So assuming your line has an end, it will match :-)

If you want to match an actual $ in grep, use one of:

grep \\\$
grep '\$'

In the former, the shell interprets \\ as \ and \$ as $, giving \$. In the latter, it doesn't interpret it at all.


As to your question as to why \$ matches the dollar sign rather the the two-character sequence, regular expressions like those used in grep use special characters for some purposes. Some of those are:

$       end of line
^       start of line
.       any character
+       1 or more of the preceeding pattern
*       0 or more of the preceeding pattern
{n,m}   between n and m of the preceeding pattern
[x-y]   any character between x and y (such as [0-9] for a digit).

along with many others.

When you want to match a literla character that's normally treated as a special character, you need to escape it so that grep will treat it as the normal character.

share|improve this answer
1  
to test what grep sees, try echo \$ and then echo \\$ –  Pasi Savolainen Apr 15 '10 at 12:53
    
In grep \\\$, as you said, \$ is passed to grep so it should match strings which contain \$, but why is it matching those string which only contain a $ ? –  Happy Mittal Apr 15 '10 at 13:00
    
Because $ in grep means an end of line. To match the character $, you need to escape it so grep doesn't treat it specially. See my update. –  paxdiablo Apr 15 '10 at 13:21
    
@Pasi: echo does its own unescaping, depending on whether it's a shell builtin or not, POSIX compliant or not, etc. Better is (IMHO) to use ls \$ and see that we get "ls: $: No such file or directory", to see that \$ is unescaped to $ but \\$ is unescaped to \$, and so on. –  Henno Jul 29 '10 at 21:16

The shell first expands any escape sequences before passing the arguments to the program, so it interprets the \$ as an escape sequence and passes the single argument $ to grep, which matches the end of line. Since every line has an end, then any line should match :)

share|improve this answer
    
But How $ is end of line ? Isn't the end of line is \n ? –  Happy Mittal Apr 15 '10 at 12:53
2  
@Happy Mithal: See man grep; it has been decided that $ matches the end of the line, regardless of whether or not the lines are separated by \n, \r, both, or something entirely different. In the general case, regular expressions can be used to match any string, not just a “line”, and $ is used to match the end of the string. –  Arkku Apr 15 '10 at 12:59

It's being interpreted as the end-of-line metacharacter. If you want to match an actual dollar sign, do

 $ grep \\$ 

or

 $ grep '\$' 
share|improve this answer

^ is for start of string, and $ for end.

grep \$

or

grep $

will match every string, so anything you type is echoed back.

Try

grep a$

Now, only strings whose last character is a will be matched and echoed.

share|improve this answer

You can save the trouble of escaping if you want to search for a literal $, by using the regular expression for the character class. For example,

$ grep '[$]' file
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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