coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

"expr" won't match empty strings


From: Luke Kendall
Subject: "expr" won't match empty strings
Date: Sat, 02 Aug 2014 15:03:01 +1000
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.24) Gecko/20101027 Thunderbird/2.0.0.24 Mnenhy/0.7.6.666

I'm hesitant to report this, but I think it's an actual bug in expr that's been there from day one.

I believe that expr, when used to match regular expressions, should use the success/failure of the pattern match to determine the exit code.

But instead, I believe "expr" uses the length of the matched string to determine its exit code. So when the regexp correctly matches an empty string, expr returns failure, despite the match. Here's a simple example:

$ expr " " : "^ *$" && echo Matched.
1
Matched.
$ expr "" : "^ *$" && echo Matched.
0

And compare that to what sed and grep do:


$ echo "" | sed -n 's/^,*$/& - yep/p'
 - yep

$ printf "a\n\n" | grep '^$' && echo "A match."

A match.

I'd like to suggest that expr be changed to use the success/fail of the pattern match to determine the exit status, as all the other unix tools do.

I don't think this alteration of semantics would break many existing scripts, for two reasons: 1) It must be unusual to use regexps that can match an empty string, because expr does not report a match for that corner case, so to correctly handle it, the user must have had to add an explicit test for the input string being empty: and this will still work (it's just that with the suggested change, that extra code becomes redundant). 2) Based on my own experience, it's unusual to use expr ":" with patterns that can match the empty string - it's taken me over 30 years to notice this oddity!

If you think this would be a good change, but don't have time to do anything, let me know and I'll have a go and submit a patch.

Regards,

luke



reply via email to

[Prev in Thread] Current Thread [Next in Thread]