On 11/26/25 7:32 PM, [email protected] wrote:
Configuration Information [Automatically generated, do not change]:
Machine: aarch64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS: -g -O2 -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer>
uname output: Linux aelata-vbox 6.8.0-88-generic #89-Ubuntu SMP PREEMPT_DYNAMIC>
Machine Type: aarch64-unknown-linux-gnu

Bash Version: 5.2
Patch Level: 21
Release Status: release

Description:
When BASH_COMPAT=3.2 is set on bash 5.2, quoting '&' in pattern substitution is 
not compatible with bash 3.2.

Thanks for the report. This is the result of the `patsub_replacement' shell
option, which was introduced in bash-5.2, so most of the testing in your
message is a red herring.

This is a combination of two compatibility features. The compatibility
level that makes the difference is 4.2:

compat42
        - the replacement string in double-quoted pattern substitution is not
          run through quote removal, as it is in versions after bash-4.2

(that's not precisely relevant, since you didn't double-quote the word
expansion, but it serves our purpose here to identify the version).

The effect of this is that the replacement string in bash-5.2 without any
compatibility mode set is "\\<" and "<" in bash-4.2 compatibility
mode (the function literally uses the bash-4.2 code), as it is in bash-4.2.

Since you have patsub_replacement set, and the `&' in the replacement
string is not escaped, it gets replaced with the match, and you get
"a <lt; b" as the result.

You don't appear to want the replacement in this case, so it seems like
an easy workaround is to run `shopt -u patsub_replacement' before executing
this section of code in whatever your real script is.

The other question is what, if anything, to do about this in bash. Maybe
the right thing, since the bash-4.2 code doesn't do anything to quote the
ampersand specially, is to simply disable `&' replacement if the shell
compatibility level is <= 42. Another option would be to quote the
ampersand appropriately if the compatibility level is <= 42. Or, since
bash-4.2 dates from 2010, maybe the best thing is just to move forward.

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

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature

Reply via email to