On Thu, Jun 19, 2025, at 5:28 PM, Jeff Ketchum wrote:
> $ cat replacestring.sh
> original_string="1|2|3|4"
> replace_string=':\\'
> echo "original: ${original_string} replace:${replace_string}"
> echo "unquoted ${original_string/2/${replace_string}}"
> echo "quoted   ${original_string/2/"${replace_string}"}"
>
> [...]
>
> on older versions, this was a little different:
> GNU bash, version 4.1.2(2)-release (x86_64-redhat-linux-gnu)
> $ bash /tmp/replacestring.sh
> original: 1|2|3|4 replace::\\
> unquoted 1|:\\|3|4
> quoted   1|":\\"|3|4
>
>
> Then, there was an inbetween version: (custom but based on this)
> GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-musl)
> original: 1|2|3|4 replace::\\
> unquoted 1|:\\|3|4
> quoted   1|:\\|3|4

This changed in bash 4.3:

        There is one incompatible change between bash-4.2 and
        bash-4.3.  Bash now performs quote removal on the replacement
        string in pattern substitution (${param/pat/rep}), since
        the shell treats quotes as special.  If you have to quote
        single quotes to get them to be treated literally, the shell
        should perform quote removal on them.

https://lists.gnu.org/archive/html/bug-bash/2014-02/msg00081.html

> [...]
>
> GNU bash, version 5.2.37(1)-release (x86_64-pc-linux-gnu)
> $ bash replacestring.sh
> original: 1|2|3|4 replace::\\
> unquoted 1|:\|3|4
> quoted   1|:\\|3|4

This changed in bash 5.2:

        There is a new shell option, `patsub_replacement'.  When
        enabled, a `&' in the replacement string of the pattern
        substitution expansion is replaced by the portion of the
        string that matched the pattern.  Backslash will escape the
        `&' and insert a literal `&'.  This option is enabled by
        default.

(Since '\' is now an escape character, the fact that it escapes
itself is implied.)

https://lists.gnu.org/archive/html/bug-bash/2022-09/msg00056.html

-- 
vq

Reply via email to