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 |
|
---|
14 | v='a*b\*c'
|
---|
15 | echo 'Source: ' "$v"
|
---|
16 | echo 'Replace str: ' '_\\_\z_'
|
---|
17 |
|
---|
18 | echo 'Pattern: ' 'single backslash and star: "replace literal star"'
|
---|
19 | echo 'Unquoted: ' ${v/\*/_\\_\z_}
|
---|
20 | r=${v/\*/_\\_\z_}
|
---|
21 | echo 'Unquoted =: ' "$r"
|
---|
22 | echo 'Quoted: ' "${v/\*/_\\_\z_}"
|
---|
23 | r="${v/\*/_\\_\z_}"
|
---|
24 | echo 'Quoted =: ' "$r"
|
---|
25 |
|
---|
26 | echo 'Pattern: ' 'double backslash and star: "replace backslash and everything after it"'
|
---|
27 | echo 'Unquoted: ' ${v/\\*/_\\_\z_}
|
---|
28 | r=${v/\\*/_\\_\z_}
|
---|
29 | echo 'Unquoted =: ' "$r"
|
---|
30 | echo 'Quoted: ' "${v/\\*/_\\_\z_}"
|
---|
31 | r="${v/\\*/_\\_\z_}"
|
---|
32 | echo 'Quoted =: ' "$r"
|
---|
33 |
|
---|
34 | echo
|
---|
35 |
|
---|
36 | v='a\bc'
|
---|
37 | echo 'Source: ' "$v"
|
---|
38 | echo 'Replace str: ' '_\\_\z_'
|
---|
39 |
|
---|
40 | echo 'Pattern: ' 'single backslash and b: "replace b"'
|
---|
41 | echo 'Unquoted: ' ${v/\b/_\\_\z_}
|
---|
42 | r=${v/\b/_\\_\z_}
|
---|
43 | echo 'Unquoted =: ' "$r"
|
---|
44 | echo 'Quoted: ' "${v/\b/_\\_\z_}"
|
---|
45 | r="${v/\b/_\\_\z_}"
|
---|
46 | echo 'Quoted =: ' "$r"
|
---|
47 |
|
---|
48 | echo 'Pattern: ' 'double backslash and b: "replace backslash and b"'
|
---|
49 | echo 'Unquoted: ' ${v/\\b/_\\_\z_}
|
---|
50 | r=${v/\\b/_\\_\z_}
|
---|
51 | echo 'Unquoted =: ' "$r"
|
---|
52 | echo 'Quoted: ' "${v/\\b/_\\_\z_}"
|
---|
53 | r="${v/\\b/_\\_\z_}"
|
---|
54 | echo 'Quoted =: ' "$r"
|
---|
55 |
|
---|
56 | echo
|
---|
57 |
|
---|
58 | v='a\bc'
|
---|
59 | s='_\\_\z_'
|
---|
60 | echo 'Source: ' "$v"
|
---|
61 | echo 'Replace str: ' "$s" '(as variable $s)'
|
---|
62 |
|
---|
63 | echo 'Pattern: ' 'single backslash and b: "replace b"'
|
---|
64 | echo 'Unquoted: ' ${v/\b/$s}
|
---|
65 | r=${v/\b/$s}
|
---|
66 | echo 'Unquoted =: ' "$r"
|
---|
67 | echo 'Quoted: ' "${v/\b/$s}"
|
---|
68 | r="${v/\b/$s}"
|
---|
69 | echo 'Quoted =: ' "$r"
|
---|
70 |
|
---|
71 | echo 'Pattern: ' 'double backslash and b: "replace backslash and b"'
|
---|
72 | echo 'Unquoted: ' ${v/\\b/$s}
|
---|
73 | r=${v/\\b/$s}
|
---|
74 | echo 'Unquoted =: ' "$r"
|
---|
75 | echo 'Quoted: ' "${v/\\b/$s}"
|
---|
76 | r="${v/\\b/$s}"
|
---|
77 | echo 'Quoted =: ' "$r"
|
---|
78 |
|
---|
79 | echo
|
---|
80 |
|
---|
81 | echo Done: $?
|
---|