1# This testcase demonstrates that backslashes are treated differently 2# in 1st and 2nd parts of ${var/search/repl}: 3# if quoted ("${var/search/repl}"), and repl contains \a (a non-special char), 4# the backslash in repl stays; if unquoted, backslash is removed. 5# But search part does not act like that: \a is always converted to just a, 6# even in quotes. 7# 8# bash4 (and probably bash3 too): "Quoted:" results are different from 9# unquoted expansions - they have a backslash before z. 10# 11# The difference only exists if repl is a literal. If it is a variable: 12# ${v/.../$s}, then all backslashes are preserved in both cases. 13 14v='a*b\*c' 15echo 'Source: ' "$v" 16echo 'Replace str: ' '_\\_\z_' 17 18echo 'Pattern: ' 'single backslash and star: "replace literal star"' 19echo 'Unquoted: ' ${v/\*/_\\_\z_} 20r=${v/\*/_\\_\z_} 21echo 'Unquoted =: ' "$r" 22echo 'Quoted: ' "${v/\*/_\\_\z_}" 23r="${v/\*/_\\_\z_}" 24echo 'Quoted =: ' "$r" 25 26echo 'Pattern: ' 'double backslash and star: "replace backslash and everything after it"' 27echo 'Unquoted: ' ${v/\\*/_\\_\z_} 28r=${v/\\*/_\\_\z_} 29echo 'Unquoted =: ' "$r" 30echo 'Quoted: ' "${v/\\*/_\\_\z_}" 31r="${v/\\*/_\\_\z_}" 32echo 'Quoted =: ' "$r" 33 34echo 35 36v='a\bc' 37echo 'Source: ' "$v" 38echo 'Replace str: ' '_\\_\z_' 39 40echo 'Pattern: ' 'single backslash and b: "replace b"' 41echo 'Unquoted: ' ${v/\b/_\\_\z_} 42r=${v/\b/_\\_\z_} 43echo 'Unquoted =: ' "$r" 44echo 'Quoted: ' "${v/\b/_\\_\z_}" 45r="${v/\b/_\\_\z_}" 46echo 'Quoted =: ' "$r" 47 48echo 'Pattern: ' 'double backslash and b: "replace backslash and b"' 49echo 'Unquoted: ' ${v/\\b/_\\_\z_} 50r=${v/\\b/_\\_\z_} 51echo 'Unquoted =: ' "$r" 52echo 'Quoted: ' "${v/\\b/_\\_\z_}" 53r="${v/\\b/_\\_\z_}" 54echo 'Quoted =: ' "$r" 55 56echo 57 58v='a\bc' 59s='_\\_\z_' 60echo 'Source: ' "$v" 61echo 'Replace str: ' "$s" '(as variable $s)' 62 63echo 'Pattern: ' 'single backslash and b: "replace b"' 64echo 'Unquoted: ' ${v/\b/$s} 65r=${v/\b/$s} 66echo 'Unquoted =: ' "$r" 67echo 'Quoted: ' "${v/\b/$s}" 68r="${v/\b/$s}" 69echo 'Quoted =: ' "$r" 70 71echo 'Pattern: ' 'double backslash and b: "replace backslash and b"' 72echo 'Unquoted: ' ${v/\\b/$s} 73r=${v/\\b/$s} 74echo 'Unquoted =: ' "$r" 75echo 'Quoted: ' "${v/\\b/$s}" 76r="${v/\\b/$s}" 77echo 'Quoted =: ' "$r" 78 79echo 80 81echo Done: $? 82