help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: help with pattern matching needed


From: Christoph Anton Mitterer
Subject: Re: help with pattern matching needed
Date: Fri, 07 Jan 2022 23:15:31 +0100
User-agent: Evolution 3.42.2-1

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.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]