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: Chet Ramey
Subject: Re: help with pattern matching needed
Date: Fri, 7 Jan 2022 12:05:27 -0500
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Thunderbird/91.4.1

On 1/7/22 2:20 AM, Lawrence Velázquez wrote:



Case 2:
*******
A string like:
[.*^$[\]
should end up (after quote removal) as the pattern:
[.*^$[]
an AFAIU be valid (but of course not match the literal \), but bash
complains about a missing matching ].
That construct executes e.g. in (d)ash, though it never matches (or at
least not with the plain single characters).

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.


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.


--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/



reply via email to

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