[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Doubt about expr
From: |
Paul Eggert |
Subject: |
Re: Doubt about expr |
Date: |
Wed, 13 Sep 2006 06:17:23 -0700 |
User-agent: |
Gnus/5.1008 (Gnus v5.10.8) Emacs/21.4 (gnu/linux) |
"Sebastian Kreft" <address@hidden> writes:
> I don't know if it is a standard of expr, but I think that is not
> intuitive to write a plus symbol before the string substr if I want
> to get the length of it. I know that is in the help, but I think
> that this shouldn't be necessary.
Traditional Unix reports a syntax error if you omit the "+". E.g., on
Solaris 8:
$ /bin/expr length substr
expr: syntax error
$ /usr/xpg4/bin/expr length substr
expr: syntax error
The "+" is a GNU extension; it avoids the ambiguities involved in
treating strings like "substr" both as operators and as data.
For example:
expr length match = 1
Should this be parsed as "(length match) = 1" or as "length (match =
1)"? Your intuition might go one way, but "expr" might go the other.
For what it's worth, Unix expr reports a syntax error for this 2nd
example as well; GNU expr parses it in the 2nd way.
> I haven't seen the source code of the parsing so much, because I don't know
> if this behaviour is a standard or not.
Both GNU expr and Unix expr conform to the POSIX standard, as far as I
know, since POSIX says only "The use of string arguments length,
substr, index, or match produces unspecified results." See
<http://www.opengroup.org/onlinepubs/000095399/utilities/expr.html>.
My own feeling is that portable programs should not use "length",
"substr", "index", or "match", as they cause too much trouble. You
can use Awk if you want string processing.