blob: 32aa2b34ce48098f3420912c0a89d2fb49b47a8a [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
Denys Vlasenkoe3354bc2016-10-02 18:08:04 +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 Vlasenkof56fe822010-08-06 17:21:52 +020013
14v='a*b\*c'
15echo 'Source: ' "$v"
16echo 'Replace str: ' '_\\_\z_'
17
18echo 'Pattern: ' 'single backslash and star: "replace literal star"'
Denys Vlasenkof56fe822010-08-06 17:21:52 +020019echo 'Unquoted: ' ${v/\*/_\\_\z_}
Denys Vlasenkoe3354bc2016-10-02 18:08:04 +020020r=${v/\*/_\\_\z_}
21echo 'Unquoted =: ' "$r"
Denys Vlasenkof56fe822010-08-06 17:21:52 +020022echo 'Quoted: ' "${v/\*/_\\_\z_}"
Denys Vlasenkoe3354bc2016-10-02 18:08:04 +020023r="${v/\*/_\\_\z_}"
24echo 'Quoted =: ' "$r"
Denys Vlasenkof56fe822010-08-06 17:21:52 +020025
26echo 'Pattern: ' 'double backslash and star: "replace backslash and everything after it"'
Denys Vlasenkof56fe822010-08-06 17:21:52 +020027echo 'Unquoted: ' ${v/\\*/_\\_\z_}
Denys Vlasenkoe3354bc2016-10-02 18:08:04 +020028r=${v/\\*/_\\_\z_}
29echo 'Unquoted =: ' "$r"
Denys Vlasenkof56fe822010-08-06 17:21:52 +020030echo 'Quoted: ' "${v/\\*/_\\_\z_}"
Denys Vlasenkoe3354bc2016-10-02 18:08:04 +020031r="${v/\\*/_\\_\z_}"
32echo 'Quoted =: ' "$r"
Denys Vlasenkof56fe822010-08-06 17:21:52 +020033
34echo
35
36v='a\bc'
37echo 'Source: ' "$v"
38echo 'Replace str: ' '_\\_\z_'
39
Denys Vlasenko33bbb272010-08-07 22:24:36 +020040echo 'Pattern: ' 'single backslash and b: "replace b"'
Denys Vlasenkof56fe822010-08-06 17:21:52 +020041echo 'Unquoted: ' ${v/\b/_\\_\z_}
Denys Vlasenkoe3354bc2016-10-02 18:08:04 +020042r=${v/\b/_\\_\z_}
43echo 'Unquoted =: ' "$r"
Denys Vlasenkof56fe822010-08-06 17:21:52 +020044echo 'Quoted: ' "${v/\b/_\\_\z_}"
Denys Vlasenkoe3354bc2016-10-02 18:08:04 +020045r="${v/\b/_\\_\z_}"
46echo 'Quoted =: ' "$r"
Denys Vlasenkof56fe822010-08-06 17:21:52 +020047
48echo 'Pattern: ' 'double backslash and b: "replace backslash and b"'
Denys Vlasenkof56fe822010-08-06 17:21:52 +020049echo 'Unquoted: ' ${v/\\b/_\\_\z_}
Denys Vlasenkoe3354bc2016-10-02 18:08:04 +020050r=${v/\\b/_\\_\z_}
51echo 'Unquoted =: ' "$r"
Denys Vlasenkof56fe822010-08-06 17:21:52 +020052echo 'Quoted: ' "${v/\\b/_\\_\z_}"
Denys Vlasenkoe3354bc2016-10-02 18:08:04 +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 Vlasenkof56fe822010-08-06 17:21:52 +020078
79echo
80
Denys Vlasenkob0fbe4b2010-08-05 17:19:27 +020081echo Done: $?