[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: |
Sat, 8 Jan 2022 12:55:51 -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 5:15 PM, Christoph Anton Mitterer wrote:
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?
Correct.
Is that deprecated form actually scheduled to go away?
Not scheduled, no.
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 ] ?
There is no invalid pattern. There is only a pattern that doesn't contain
a bracket expression. It might not be what you intend, but it's valid.
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?
There was a pretty spirited argument about that. (The messages in the URL
below don't capture the entire discussion.)
https://www.austingroupbugs.net/view.php?id=1234
You can see in the proposed interpretation how difficult it is to capture
the intended behavior.
--
``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/
- help with pattern matching needed, Christoph Anton Mitterer, 2022/01/07
- Re: help with pattern matching needed, Christoph Anton Mitterer, 2022/01/07
- Re: help with pattern matching needed, Chet Ramey, 2022/01/08
- Re: help with pattern matching needed, Christoph Anton Mitterer, 2022/01/10
- Re: help with pattern matching needed, Christoph Anton Mitterer, 2022/01/11
- Re: help with pattern matching needed, Lawrence Velázquez, 2022/01/11
- Re: help with pattern matching needed, Christoph Anton Mitterer, 2022/01/11
- Re: help with pattern matching needed, Christoph Anton Mitterer, 2022/01/12
- Re: help with pattern matching needed, Christoph Anton Mitterer, 2022/01/12