On 6/19/25 7:16 PM, Jeff Ketchum wrote:
I think that helps me understand the differences better, and what I am
seeing.

Though It doesn't seem like it is completely consistent, and not what I
expected when using a variable with specific layout. (and also breaking
change enabled by default)

example, if i change the replacement to '\a'
it stays as \a
$ bash replacestring.sh
original: 1|2|3|4 replace:\a
unqouted 1|\a|3|4
qouted   1|\a|3|4

so, it seems it only escapes it if its a double backslash, or escaping a &
and it is different again, if i change the script to do \a manually

$ cat replacestring.sh
original_string="1|2|3|4"
replace_string='\a'
echo "original: ${original_string} replace:${replace_string}"
echo "unqouted ${original_string/2/${replace_string}}"
echo "qouted   ${original_string/2/"${replace_string}"}"
echo "manual   ${original_string/2/\a}"

output for newer bash:
...
manual   1|a|3|4

This is the change between bash-4.2 and bash-4.3 that added quote removal.


this is slightly different behaviour from the variable, but in older
versions, it shows it
GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)
$ bash /tmp/replacestring.sh
original: 1|2|3|4 replace:\a
unqouted 1|\a|3|4
qouted   1|"\a"|3|4
manual   1|\a|3|4


It could also be a double quote, '\"' that is escaped, and it doesn't
interpret the \ as an escape, which is a character that I would expect to
see that happen for.

Quote removal doesn't happen on the results of variable expansion -- an
expansion that contains a quote character doesn't have the quote character
removed.

also, a single \ doesn't have to be quoted either
replace_string='\'

Do you mean in the assignment statement? Backslash has no effect as an
escape character within single quotes.

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

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature

Reply via email to