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 and assignment expansions - they have a backslash before z.
|
---|
10 |
|
---|
11 | v='a*b\*c'
|
---|
12 | echo 'Source: ' "$v"
|
---|
13 | echo 'Replace str: ' '_\\_\z_'
|
---|
14 |
|
---|
15 | echo 'Pattern: ' 'single backslash and star: "replace literal star"'
|
---|
16 | r=${v/\*/_\\_\z_}
|
---|
17 | echo 'In assignment:' "$r"
|
---|
18 | echo 'Unquoted: ' ${v/\*/_\\_\z_}
|
---|
19 | echo 'Quoted: ' "${v/\*/_\\_\z_}"
|
---|
20 |
|
---|
21 | echo 'Pattern: ' 'double backslash and star: "replace backslash and everything after it"'
|
---|
22 | r=${v/\\*/_\\_\z_}
|
---|
23 | echo 'In assignment:' "$r"
|
---|
24 | echo 'Unquoted: ' ${v/\\*/_\\_\z_}
|
---|
25 | echo 'Quoted: ' "${v/\\*/_\\_\z_}"
|
---|
26 |
|
---|
27 | echo
|
---|
28 |
|
---|
29 | v='a\bc'
|
---|
30 | echo 'Source: ' "$v"
|
---|
31 | echo 'Replace str: ' '_\\_\z_'
|
---|
32 |
|
---|
33 | echo 'Pattern: ' 'single backslash and b: "replace b"'
|
---|
34 | r=${v/\b/_\\_\z_}
|
---|
35 | echo 'In assignment:' "$r"
|
---|
36 | echo 'Unquoted: ' ${v/\b/_\\_\z_}
|
---|
37 | echo 'Quoted: ' "${v/\b/_\\_\z_}"
|
---|
38 |
|
---|
39 | echo 'Pattern: ' 'double backslash and b: "replace backslash and b"'
|
---|
40 | r=${v/\\b/_\\_\z_}
|
---|
41 | echo 'In assignment:' "$r"
|
---|
42 | echo 'Unquoted: ' ${v/\\b/_\\_\z_}
|
---|
43 | echo 'Quoted: ' "${v/\\b/_\\_\z_}"
|
---|
44 |
|
---|
45 | echo
|
---|
46 |
|
---|
47 | echo Done: $?
|
---|