On Tue, 2008-04-15 at 23:00 +0200, Denys Vlasenko wrote:
> Well, we are trying to emulate bash here, and so far it does not fully
> work:
I don't think we should be trying to emulate bash in this case. This
feature (#) is a POSIX standard feature, not a bash-ism, and it should
be supported by ash as described in the POSIX standard--which may or may
not be as bash implements it.
> # ./busybox ash -c "runlevel='init [2]';"' echo $runlevel; echo ${runlevel#*
> [}'
> init [2]
> init [2]
> # bash -c "runlevel='init [2]';"' echo $runlevel; echo ${runlevel#* [}'
> init [2]
> 2]
I've checked the standard and I think, but am not 100% sure, that ash is
actually correct here. I believe that the "[" is required to be escaped
in this situation. Note that, as someone else mentioned, dash behaves
the same way as ash in this situation.
The standard says:
${parameter#word}
Remove Smallest Prefix Pattern. The word shall be
expanded to produce a pattern. The parameter expansion
shall then result in parameter, with the smallest
portion of the prefix matched by the pattern deleted.
So, then we look for the definition of a pattern:
When unquoted and outside a bracket expression, the following
three characters shall have special meaning in the specification
of patterns:
...
[
The open bracket shall introduce a pattern bracket
expression.
It doesn't give any indication that a closing bracket is required: just
that the open bracket introduces a pattern bracket expression.
Later we see defined the common globbing rule that if the pattern is
illegal, it will be left as-is:
If the pattern contains an invalid bracket expression or does
not match any existing filenames or pathnames, the pattern
string shall be left unchanged.
_However_, that comment is in a section titled
"2.13.3 Patterns Used for Filename Expansion", and it explicitly states
there that it applies only to patterns used for filename expansion.
This pattern is not one such, so we can't assume that this rule applies
there.
So, the standard doesn't actually say one way or the other definitively
as far as I can tell, but to me it seems like ash's current behavior
complies with the standard.
> # ./busybox ash -c "runlevel='init [2]';"' echo $runlevel; echo ${runlevel#*
> \[}'
> init [2]
> 2]
Adding the backslash here is definitely the right thing to do; it
guarantees that your pattern will match properly regardless of how the
standard is interpreted here.
_______________________________________________
busybox mailing list
[email protected]
http://busybox.net/cgi-bin/mailman/listinfo/busybox