[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: string substitution broken since 5.2

From: Koichi Murase
Subject: Re: string substitution broken since 5.2
Date: Fri, 4 Nov 2022 04:09:10 +0900

2022年11月4日(金) 3:46 Greg Wooledge <greg@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//\"/\&quot;}"
> 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//\"/\&quot;}; 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:



reply via email to

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