2022年11月4日(金) 3:46 Greg Wooledge <g...@wooledge.org>: > On Fri, Nov 04, 2022 at 03:31:44AM +0900, Koichi Murase wrote: > > [...] > > > > Another option is to quote `&' in the replacement if you want to make > > it work regardless of the 5.2 feature, patsub_replacement: > > > > string="${string//\"/\"}" > > Sadly, bash 5.2 breaks backward compatibility in ways that are highly > inconvenient. There's essentially no way you can write this that will > work with bash 4.2, bash 4.3-5.1, and bash 5.2. > > [,,,] > > In another 10 years or so, this may not matter so much, as most instances > of bash 4.2 and earlier will have been upgraded or retired. But for now, > it's a pretty significant issue.
When one wants to also support Bash 4.2, one possible workaround is to assign the result to a variable (without quoting the right-hand side of the assignment): $ bash-4.2 -c 'string=\"hi\"; string=${string//\"/\"}; echo "$string"' $ bash-4.2 -c 'str="a string"; rep="a&b"; str=${str//a/"$rep"}; echo "$str"' These work as expected in all Bash versions I can test (2.0..5.2). I used this approach in one of my fixes related to patsub_replacement: https://github.com/akinomyoga/ble.sh/commit/a75bb25a3255fd87ba73f0f1a5ebedc23fcc71b4 -- Koichi