Bug 203279

Summary: /bin/expr operator : fails when the string has a leading hyphen.
Product: Base System Reporter: egilb
Component: binAssignee: freebsd-bugs (Nobody) <bugs>
Status: New ---    
Severity: Affects Some People CC: jilles
Priority: ---    
Version: 10.2-RELEASE   
Hardware: Any   
OS: Any   

Description egilb 2015-09-23 09:58:51 UTC
expr on other *NIX versions seems to handle a leading hyphen in the text string.
On FreeBSD, the hyphen is parsed as an illegal option:

/bin/expr '-XXabs' : '.*\(aa*\)'
expr: illegal option -- X
expr: usage: expr [-e] expression

A trivial workaround is to use grouping:

/bin/expr \( '-XXabs' : '.*\(aa*\)' \)
a
Comment 1 Jilles Tjoelker freebsd_committer freebsd_triage 2015-10-18 19:25:38 UTC
If options that take an option-argument of ":" can be excluded forever, a simple workaround would be to skip option processing if there are 3 arguments (excluding argv[0]) and the second argument is ":".

A stronger form of only treating "--" and "-e" as options would prevent adding any more options without compatibility problems.

Perhaps this is acceptable, since expr is not a utility where major extensions are expected.