On Fri, 2022-01-07 at 12:05 -0500, Chet Ramey wrote:
> > It looks like \] is being treated as a literal ] in both cases.
> > The difference seems to be in the parsing: dash gives up on the
> > bracket expression, while bash consumes the rest of the script
> > trying to close it.
> >
> > Â Â Â Â % cat ex1.sh
> > Â Â Â Â case $1 in
> > Â Â Â Â Â Â Â Â [.*^$[\]) printf '%s matched\n' "$1" ;;
> > Â Â Â Â Â Â Â Â *) printf "%s didn't match\\n" "$1" ;;
> > Â Â Â Â esac
>
> The parser sees the (deprecated) `$[' and tries to read an entire
> `$[]'
> expansion as part of a word. It fails to close that expansion and
> reports
> an EOF error.
So the failure is not because of the missing ] for the pattern, but the
missing ] for the $[], right?
Is that deprecated form actually scheduled to go away?
> > That is it, though. The empty $[] substitutes "0" into the
> > pattern.
> >
> > Â Â Â Â % cat ex2.sh
> > Â Â Â Â case $1 in
> > Â Â Â Â Â Â Â Â [.*^$[]) printf '%s matched\n' "$1" ;;
> > Â Â Â Â Â Â Â Â *) printf "%s didn't match\\n" "$1" ;;
> > Â Â Â Â esac
> > Â Â Â Â % bash ex2.sh '[.xxxxxxx^0'
> > Â Â Â Â [.xxxxxxx^0 matched
> >
> > However, as you noted, bash doesn't parse quite so greedily this
> > time. I don't know why not.
>
> Because the closing `]' isn't escaped and the $[] expansion is
> complete.
Shouldn't it then be made failing because of the invalid pattern, that
misses an ] ?
That would also give people a better chance to find the thing with $[].
And is it possible to get the whole thing better documented? I mean I
probably won't be the last one to stumble over it.
And from reading either POSIX or Bash manpage I had already the
impression that there would be a 2 stage unescaping... first the normal
quote removal, then making special characters in patterns (*?[\) loose
their special meaning?
Or could that be changed, should it violate POSIX?
Thanks,
Chris.