expand: Recognize '^' as a negation character in BE
PATCH 1/1 expand: Recognize '^' as a negation character in BE
While parsing bracket expression ('[...]'), DASH recognizes only '!' as a special character for negation/inversion, but POSIX specifies '^'.
The POSIX specification (2018 edition) states:
^ The <circumflex> shall signify a non-matching list expression when
it occurs first in a list, immediately following a <left-square-bracket> (see RE Bracket Expression).
DASH:
$ i='123 asd' && printf "%s\n" "${i##*[!a-z]}"
asd
$ i='123 asd' && printf "%s\n" "${i##*[^a-z]}"
<empty expansion>BASH (with --posix):
$ i='123 asd' && printf "%s\n" "${i##*[!a-z]}"
asd
$ i='123 asd' && printf "%s\n" "${i##*[^a-z]}"
asdMake <circumflex> ('^') a special character used to specify negation/inversion in bracket expressions:
$ i='123 asd' && printf "%s\n" "${i##*[^a-z]}"
asd--- src/expand.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
src/expand.c +1 -1
@@ -1565,7 +1565,7 @@ pmatch(const char *pattern, const char *string) startp = p; invert =0 ; -if (*p =='!' ) { +if (*p =='!' || *p =='^' ) { invert++; p++; }
Signature · 1 line
2.32.0
Quoted 8 lines · Dimitar Yurukov
While parsing bracket expression ('[...]'), DASH recognizes only '!' as a special character for negation/inversion, but POSIX specifies '^'.
The POSIX specification (2018 edition) states:
^ The <circumflex> shall signify a non-matching list expression when
it occurs first in a list, immediately following a <left-square-bracket> (see RE Bracket Expression).
It also states:
the <exclamation-mark> character ( '!' ) shall replace the
<circumflex> character ( '^' ) in its role in a non-matching list in the regular expression notation
and
A bracket expression starting with an unquoted <circumflex> character
produces unspecified results.
See 2.13.1 Patterns Matching a Single Character.
So both the dash and the bash behaviour are permitted and this patch does not address a correctness issue. Scripts that rely on ^ for negation should be modified to use !.
The patch may still be worthwhile to increase compatibility, but in that case the same change also needs to be made to expmeta().
Cheers,
Harald van Dijk
Quoted 16 lines · Harald van Dijk
Quoted 8 lines · Dimitar Yurukov
While parsing bracket expression ('[...]'), DASH recognizes only '!' as a special character for negation/inversion, but POSIX specifies '^'.
The POSIX specification (2018 edition) states:
^ The <circumflex> shall signify a non-matching list expression when
it occurs first in a list, immediately following a <left-square-bracket> (see RE Bracket Expression).It also states:
the <exclamation-mark> character ( '!' ) shall replace the
<circumflex> character ( '^' ) in its role in a non-matching list in the regular expression notation
Quoted 6 lines
and
A bracket expression starting with an unquoted <circumflex> character
produces unspecified results.See 2.13.1 Patterns Matching a Single Character.
Oh, my bad, sorry for the noise.
Quoted 2 lines
The patch may still be worthwhile to increase compatibility, but in that case the same change also needs to be made to
expmeta().
Oops, you are right. Will attach v2.
PATCH v2 1/1 expand: Recognize '^' as a negation character in BE
In reply to Dimitar YurukovWhile performing bracket expression ('[...]'), DASH recognizes only '!' as a special character for negation/inversion, but POSIX specifies '^'.
The specification (2018 edition):
"
9.3.3 BRE Special Characters
^ The <circumflex> shall signify a non-matching list expression when it
occurs first in a list, immediately following a <left-square-bracket> (see RE Bracket Expression).
"
DASH:
$ i='123 asd' && printf "%s\n" "${i##*[!a-z]}"
asd
$ i='123 asd' && printf "%s\n" "${i##*[^a-z]}"$
BASH (with --posix):
$ i='123 asd' && printf "%s\n" "${i##*[!a-z]}"
asd
$ i='123 asd' && printf "%s\n" "${i##*[^a-z]}"
asd
$This patch makes <circumflex> ('^') a special character used to specify negation/inversion.
---
Changes in v2:
- Fixed the patch (patched expmeta())
src/expand.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
src/expand.c +2 -2
@@ -1296,7 +1296,7 @@ expmeta(char *name, unsigned name_len, unsigned expdir_len) metaflag =1 ; else if (*p == '[') { char *q = p + 1; - if (*q == '!') + if (*q == '!' || *q == '^') q++;for (;;) {if (*q =='\\' )
@@ -1565,7 +1565,7 @@ pmatch(const char *pattern, const char *string) startp = p; invert =0 ; -if (*p =='!' ) { +if (*p =='!' || *p =='^' ) { invert++; p++; }
Signature · 1 line
2.32.0
Quoted 11 lines · Dimitar Yurukov
While performing bracket expression ('[...]'), DASH recognizes only '!' as a special character for negation/inversion, but POSIX specifies '^'.
The specification (2018 edition):
"
9.3.3 BRE Special Characters^ The <circumflex> shall signify a non-matching list expression when it
occurs first in a list, immediately following a <left-square-bracket> (see RE Bracket Expression).
"
This is incorrect and ^ cannot be used in a portable script.
Cheers,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt