blob: d5470614bbaa35330bdbbb42c380f0a3c74aebe3 [file] [log] [blame]
Denys Vlasenko36f774a2010-09-05 14:45:38 +02001# 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
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 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
47echo Done: $?