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,
8 # bash4 (and probably bash3 too): "Quoted:" results are different from
9 # unquoted expansions - they have a backslash before z.
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.
16 echo 'Replace str: ' '_\\_\z_'
18 echo 'Pattern: ' 'single backslash and star: "replace literal star"'
19 echo 'Unquoted: ' ${v/\*/_\\_\z_}
21 echo 'Unquoted =: ' "$r"
22 echo 'Quoted: ' "${v/\*/_\\_\z_}"
24 echo 'Quoted =: ' "$r"
26 echo 'Pattern: ' 'double backslash and star: "replace backslash and everything after it"'
27 echo 'Unquoted: ' ${v/\\*/_\\_\z_}
29 echo 'Unquoted =: ' "$r"
30 echo 'Quoted: ' "${v/\\*/_\\_\z_}"
32 echo 'Quoted =: ' "$r"
38 echo 'Replace str: ' '_\\_\z_'
40 echo 'Pattern: ' 'single backslash and b: "replace b"'
41 echo 'Unquoted: ' ${v/\b/_\\_\z_}
43 echo 'Unquoted =: ' "$r"
44 echo 'Quoted: ' "${v/\b/_\\_\z_}"
46 echo 'Quoted =: ' "$r"
48 echo 'Pattern: ' 'double backslash and b: "replace backslash and b"'
49 echo 'Unquoted: ' ${v/\\b/_\\_\z_}
51 echo 'Unquoted =: ' "$r"
52 echo 'Quoted: ' "${v/\\b/_\\_\z_}"
54 echo 'Quoted =: ' "$r"
61 echo 'Replace str: ' "$s" '(as variable $s)'
63 echo 'Pattern: ' 'single backslash and b: "replace b"'
64 echo 'Unquoted: ' ${v/\b/$s}
66 echo 'Unquoted =: ' "$r"
67 echo 'Quoted: ' "${v/\b/$s}"
69 echo 'Quoted =: ' "$r"
71 echo 'Pattern: ' 'double backslash and b: "replace backslash and b"'
72 echo 'Unquoted: ' ${v/\\b/$s}
74 echo 'Unquoted =: ' "$r"
75 echo 'Quoted: ' "${v/\\b/$s}"
77 echo 'Quoted =: ' "$r"