[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Using $ in quotes with characters after it
From: |
Mike Frysinger |
Subject: |
Re: Using $ in quotes with characters after it |
Date: |
Sat, 24 Mar 2012 21:05:52 -0400 |
User-agent: |
KMail/1.13.7 (Linux/3.2.0; KDE/4.6.5; x86_64; ; ) |
On Saturday 24 March 2012 15:06:00 Dan wrote:
> This is the output after running `set -x`. It looks like the shell is
> behaving properly. It's un-escaping the question mark when it's supposed
> to be and it isn't when it isn't supposed to be. Grep shouldn't be
> producing that output though. The only explanation I can think of is that
> the wildcard is being too greedy and consuming all of the string. But from
> what I know that's not how it works.
seems correct to me. in the first two, grep gets a question mark with a
backslash. in the last one, it doesn't because the shell processed it first.
if you read the man page, it says that in a basic regular expression (which
you're doing because you didn't use -E or run egrep), a plain "?" has no
meaning -- it's just another character. grep needs to see the backslash to
treat it specially. further, the anchor characters ("$" and "^") only have
meaning if they are in the last or first position respectively. otherwise,
they're just another character to match.
hence, this command:
grep '^abc.*$\?' testfile
means "anchor to the start of the line, then match abc, then match anything,
then either anchor to the end of the line or don't". so really, "$\?" at the
end of the regexp is completely pointless.
but this command:
grep ^abc.*$\? testfile
means "anchor to the start of the line, then match abc, then match anything,
then match a dollar sign, then match a question mark".
-mike
signature.asc
Description: This is a digitally signed message part.