bin/162468: expr(1) false syntax errors
Jilles Tjoelker
jilles at stack.nl
Fri Nov 11 15:50:11 UTC 2011
The following reply was made to PR bin/162468; it has been noted by GNATS.
From: Jilles Tjoelker <jilles at stack.nl>
To: bug-followup at FreeBSD.org, egrosbein at rdtc.ru
Cc:
Subject: Re: bin/162468: expr(1) false syntax errors
Date: Fri, 11 Nov 2011 16:44:55 +0100
> [expr treats any string that looks like an operator as an operator,
> for example, expr '>' : '.*' fails]
The current behaviour of expr is allowed by POSIX (SUSv4, XCU 4
Utilities, expr). If the application passes '>', this is not a string
operand but an operator, even if that results in an invalid expression.
This is also documented in the man page.
It would be a valid extension to allow such expressions but it is not
immediately clear how it would work. For example, should
expr \( = \)
compare two strings ("0") or return a single string ("=")? And should
expr \( + \)
return "+" or raise an error?
The test utility is different in that POSIX specifies how a similar
ambiguity shall be resolved (for a limited set of cases).
Oh, and if you want to find a string length in a shell script, why don't
you just use
${#VAR}
(given that the string is in $VAR)? If you must use expr(1), do
expr \( "x$VAR" : '.*' \) - 1
as described in the man page.
--
Jilles Tjoelker
More information about the freebsd-bugs
mailing list