blob: 32aa2b34ce48098f3420912c0a89d2fb49b47a8a [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
Denys Vlasenkoc49d2d92010-09-06 10:26:37 +02009# 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.
Denys Vlasenko36f774a2010-09-05 14:45:38 +020013
14v='a*b\*c'
15echo 'Source: ' "$v"
16echo 'Replace str: ' '_\\_\z_'
17
18echo 'Pattern: ' 'single backslash and star: "replace literal star"'
Denys Vlasenko36f774a2010-09-05 14:45:38 +020019echo 'Unquoted: ' ${v/\*/_\\_\z_}
Denys Vlasenkoc49d2d92010-09-06 10:26:37 +020020r=${v/\*/_\\_\z_}
21echo 'Unquoted =: ' "$r"
Denys Vlasenko36f774a2010-09-05 14:45:38 +020022echo 'Quoted: ' "${v/\*/_\\_\z_}"
Denys Vlasenkoc49d2d92010-09-06 10:26:37 +020023r="${v/\*/_\\_\z_}"
24echo 'Quoted =: ' "$r"
Denys Vlasenko36f774a2010-09-05 14:45:38 +020025
26echo 'Pattern: ' 'double backslash and star: "replace backslash and everything after it"'
Denys Vlasenko36f774a2010-09-05 14:45:38 +020027echo 'Unquoted: ' ${v/\\*/_\\_\z_}
Denys Vlasenkoc49d2d92010-09-06 10:26:37 +020028r=${v/\\*/_\\_\z_}
29echo 'Unquoted =: ' "$r"
Denys Vlasenko36f774a2010-09-05 14:45:38 +020030echo 'Quoted: ' "${v/\\*/_\\_\z_}"
Denys Vlasenkoc49d2d92010-09-06 10:26:37 +020031r="${v/\\*/_\\_\z_}"
32echo 'Quoted =: ' "$r"
Denys Vlasenko36f774a2010-09-05 14:45:38 +020033
34echo
35
36v='a\bc'
37echo 'Source: ' "$v"
38echo 'Replace str: ' '_\\_\z_'
39
40echo 'Pattern: ' 'single backslash and b: "replace b"'
Denys Vlasenko36f774a2010-09-05 14:45:38 +020041echo 'Unquoted: ' ${v/\b/_\\_\z_}
Denys Vlasenkoc49d2d92010-09-06 10:26:37 +020042r=${v/\b/_\\_\z_}
43echo 'Unquoted =: ' "$r"
Denys Vlasenko36f774a2010-09-05 14:45:38 +020044echo 'Quoted: ' "${v/\b/_\\_\z_}"
Denys Vlasenkoc49d2d92010-09-06 10:26:37 +020045r="${v/\b/_\\_\z_}"
46echo 'Quoted =: ' "$r"
Denys Vlasenko36f774a2010-09-05 14:45:38 +020047
48echo 'Pattern: ' 'double backslash and b: "replace backslash and b"'
Denys Vlasenko36f774a2010-09-05 14:45:38 +020049echo 'Unquoted: ' ${v/\\b/_\\_\z_}
Denys Vlasenkoc49d2d92010-09-06 10:26:37 +020050r=${v/\\b/_\\_\z_}
51echo 'Unquoted =: ' "$r"
Denys Vlasenko36f774a2010-09-05 14:45:38 +020052echo 'Quoted: ' "${v/\\b/_\\_\z_}"
Denys Vlasenkoc49d2d92010-09-06 10:26:37 +020053r="${v/\\b/_\\_\z_}"
54echo 'Quoted =: ' "$r"
55
56echo
57
58v='a\bc'
59s='_\\_\z_'
60echo 'Source: ' "$v"
61echo 'Replace str: ' "$s" '(as variable $s)'
62
63echo 'Pattern: ' 'single backslash and b: "replace b"'
64echo 'Unquoted: ' ${v/\b/$s}
65r=${v/\b/$s}
66echo 'Unquoted =: ' "$r"
67echo 'Quoted: ' "${v/\b/$s}"
68r="${v/\b/$s}"
69echo 'Quoted =: ' "$r"
70
71echo 'Pattern: ' 'double backslash and b: "replace backslash and b"'
72echo 'Unquoted: ' ${v/\\b/$s}
73r=${v/\\b/$s}
74echo 'Unquoted =: ' "$r"
75echo 'Quoted: ' "${v/\\b/$s}"
76r="${v/\\b/$s}"
77echo 'Quoted =: ' "$r"
Denys Vlasenko36f774a2010-09-05 14:45:38 +020078
79echo
80
81echo Done: $?