blob: a6e98fd86388f461ded2a6ea43f6dbd18cf808a7 [file] [log] [blame]
Denys Vlasenkof56fe822010-08-06 17:21:52 +02001# This testcase demonstrates that backslashes are treated differently
2# in 1st and 2nd parts of ${var/search/repl}:
Denys Vlasenkoc8d305d2010-08-06 19:28:04 +02003# if quoted ("${var/search/repl}"), and repl contains \a (a non-special char),
4# the backslash in repl stays; if unquoted, backslash is removed.
Denys Vlasenkof56fe822010-08-06 17:21:52 +02005# But search part does not act like that: \a is always converted to just a,
6# even in quotes.
7#
Denys Vlasenkoc8d305d2010-08-06 19:28:04 +02008# bash4 (and probably bash3 too): "Quoted:" results are different from
9# unquoted and assignment expansions - they have a backslash before z.
Denys Vlasenkof56fe822010-08-06 17:21:52 +020010
11v='a*b\*c'
12echo 'Source: ' "$v"
13echo 'Replace str: ' '_\\_\z_'
14
15echo 'Pattern: ' 'single backslash and star: "replace literal star"'
16r=${v/\*/_\\_\z_}
17echo 'In assignment:' "$r"
18echo 'Unquoted: ' ${v/\*/_\\_\z_}
19echo 'Quoted: ' "${v/\*/_\\_\z_}"
20
21echo 'Pattern: ' 'double backslash and star: "replace backslash and everything after it"'
22r=${v/\\*/_\\_\z_}
23echo 'In assignment:' "$r"
24echo 'Unquoted: ' ${v/\\*/_\\_\z_}
25echo 'Quoted: ' "${v/\\*/_\\_\z_}"
26
27echo
28
29v='a\bc'
30echo 'Source: ' "$v"
31echo 'Replace str: ' '_\\_\z_'
32
33echo 'Pattern: ' 'single backslash and b: "replace literal b"'
34r=${v/\b/_\\_\z_}
35echo 'In assignment:' "$r"
36echo 'Unquoted: ' ${v/\b/_\\_\z_}
37echo 'Quoted: ' "${v/\b/_\\_\z_}"
38
39echo 'Pattern: ' 'double backslash and b: "replace backslash and b"'
40r=${v/\\b/_\\_\z_}
41echo 'In assignment:' "$r"
42echo 'Unquoted: ' ${v/\\b/_\\_\z_}
43echo 'Quoted: ' "${v/\\b/_\\_\z_}"
44
45echo
46
Denys Vlasenkob0fbe4b2010-08-05 17:19:27 +020047echo Done: $?