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: Lawrence Velázquez
Subject: Re: help with pattern matching needed
Date: Fri, 07 Jan 2022 18:01:42 -0500
User-agent: Cyrus-JMAP/3.5.0-alpha0-4526-gbc24f4957e-fm-20220105.001-gbc24f495

On Fri, Jan 7, 2022, at 5:15 PM, Christoph Anton Mitterer wrote:
> On Fri, 2022-01-07 at 12:05 -0500, Chet Ramey wrote:
>> 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?

Yes.  (I dropped the ball there; thanks to Chet for clearing it up.)


>> 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 ] ?

It's not invalid, though.  An unquoted [ can match literally.

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_13_01:

    When unquoted and outside a bracket expression, the following
    three characters shall have special meaning in the specification
    of patterns:

    ?  [...]
    *  [...]
    [  If an open bracket introduces a bracket expression as in XBD
       "RE Bracket Expression" [...] it shall introduce a pattern
       bracket expression.  [...]  Otherwise, '[' shall match the
       character itself.

Hence:

    % cat ex3.sh
    case $1 in
        [) printf "%s matched\\n" "$1" ;;
        *) printf "%s didn't match\\n" "$1" ;;
    esac
    % bash ex3.sh [
    [ matched
    % dash ex3.sh [
    [ matched
    % ksh ex3.sh [
    [ matched
    % yash ex3.sh [
    [ matched


> 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?

The current version of POSIX doesn't actually mention quote removal
in the typical sense.  (In the email I linked earlier, Chet mentioned
that there'd been some discussion about the wording.)

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_04_05

    In order from the beginning to the end of the `case` statement,
    each `pattern` that labels a `compound-list` shall be subjected
    to tilde expansion, parameter expansion, command substitution,
    and arithmetic expansion, and the result of these expansions
    shall be compared against the expansion of `word`, according
    to the rules described in "Pattern Matching Notation" (which
    also describes the effect of quoting parts of the pattern).

The current release of the Bash manual doesn't mention quote removal
either.  (The development version does, but I don't know if it makes
additional clarifications.)

https://www.gnu.org/software/bash/manual/html_node/Conditional-Constructs.html#index-case

    The match is performed according to the rules described below
    in "Pattern Matching".  [...]  Each `pattern` undergoes tilde
    expansion, parameter expansion, command substitution, and
    arithmetic expansion.

-- 
vq



reply via email to

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