Date: Fri, 11 Nov 2011 15:50:10 GMT From: Jilles Tjoelker <jilles@stack.nl> To: freebsd-bugs@FreeBSD.org Subject: Re: bin/162468: expr(1) false syntax errors Message-ID: <201111111550.pABFoAqS017464@freefall.freebsd.org>
index | next in thread | raw e-mail
The following reply was made to PR bin/162468; it has been noted by GNATS.
From: Jilles Tjoelker <jilles@stack.nl>
To: bug-followup@FreeBSD.org, egrosbein@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
help
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201111111550.pABFoAqS017464>
