bug-bash
[Top][All Lists]
Advanced

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

Re: string substitution broken since 5.2


From: Greg Wooledge
Subject: Re: string substitution broken since 5.2
Date: Thu, 3 Nov 2022 14:45:53 -0400

On Fri, Nov 04, 2022 at 03:31:44AM +0900, Koichi Murase wrote:
> 2022年11月4日(金) 2:50 Andreas Schwab <schwab@linux-m68k.org>:
> > On Nov 03 2022, thierryb--- via Bug reports for the GNU Bourne Again SHell 
> > wrote:
> > > Description:
> > >       String substitution code running for years is broken in 5.2.
> > >
> > > Repeat-By:
> > >       string = 'xdotool type "sudo apt update"'
> > >       string="${string//\"/&quot;}"
> > >       printf '%s' "$string"
> > >
> > >       previously outputs:
> > >       xdotool type &quot;sudo apt update&quot;
> > >
> > >       but now outputs:
> > >       xdotool type "quot;sudo apt update"quot;
> >
> > shopt -u patsub_replacement
> 
> 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.

unicorn:~$ bash-4.2 -c 'string=\"hi\"; echo "${string//\"/\&quot;}"'
\&quot;hi\&quot;
unicorn:~$ bash-4.3 -c 'string=\"hi\"; echo "${string//\"/\&quot;}"'
&quot;hi&quot;
unicorn:~$ bash-5.2 -c 'string=\"hi\"; echo "${string//\"/\&quot;}"'
&quot;hi&quot;

This also affects quoting/non-quoting of the replacement, if it's in a
variable.  If you DON'T quote, it works in everything up to 5.1, but
not later:

unicorn:~$ bash-4.2 -c 'str="a string"; rep="a&b"; echo "${str//a/$rep}"'
a&b string
unicorn:~$ bash-4.3 -c 'str="a string"; rep="a&b"; echo "${str//a/$rep}"'
a&b string
unicorn:~$ bash-5.2 -c 'str="a string"; rep="a&b"; echo "${str//a/$rep}"'
aab string

If you DO quote, it works in everything from 4.3 up, but not earlier:

unicorn:~$ bash-4.2 -c 'str="a string"; rep="a&b"; echo "${str//a/"$rep"}"'
"a&b" string
unicorn:~$ bash-4.3 -c 'str="a string"; rep="a&b"; echo "${str//a/"$rep"}"'
a&b string
unicorn:~$ bash-5.2 -c 'str="a string"; rep="a&b"; echo "${str//a/"$rep"}"'
a&b string

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.



reply via email to

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