Denys Vlasenko | f56fe82 | 2010-08-06 17:21:52 +0200 | [diff] [blame] | 1 | # This testcase demonstrates that backslashes are treated differently |
| 2 | # in 1st and 2nd parts of ${var/search/repl}: |
Denys Vlasenko | c8d305d | 2010-08-06 19:28:04 +0200 | [diff] [blame] | 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. |
Denys Vlasenko | f56fe82 | 2010-08-06 17:21:52 +0200 | [diff] [blame] | 5 | # But search part does not act like that: \a is always converted to just a, |
| 6 | # even in quotes. |
| 7 | # |
Denys Vlasenko | c8d305d | 2010-08-06 19:28:04 +0200 | [diff] [blame] | 8 | # bash4 (and probably bash3 too): "Quoted:" results are different from |
Denys Vlasenko | e3354bc | 2016-10-02 18:08:04 +0200 | [diff] [blame] | 9 | # 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 Vlasenko | f56fe82 | 2010-08-06 17:21:52 +0200 | [diff] [blame] | 13 | |
| 14 | v='a*b\*c' |
| 15 | echo 'Source: ' "$v" |
| 16 | echo 'Replace str: ' '_\\_\z_' |
| 17 | |
| 18 | echo 'Pattern: ' 'single backslash and star: "replace literal star"' |
Denys Vlasenko | f56fe82 | 2010-08-06 17:21:52 +0200 | [diff] [blame] | 19 | echo 'Unquoted: ' ${v/\*/_\\_\z_} |
Denys Vlasenko | e3354bc | 2016-10-02 18:08:04 +0200 | [diff] [blame] | 20 | r=${v/\*/_\\_\z_} |
| 21 | echo 'Unquoted =: ' "$r" |
Denys Vlasenko | f56fe82 | 2010-08-06 17:21:52 +0200 | [diff] [blame] | 22 | echo 'Quoted: ' "${v/\*/_\\_\z_}" |
Denys Vlasenko | e3354bc | 2016-10-02 18:08:04 +0200 | [diff] [blame] | 23 | r="${v/\*/_\\_\z_}" |
| 24 | echo 'Quoted =: ' "$r" |
Denys Vlasenko | f56fe82 | 2010-08-06 17:21:52 +0200 | [diff] [blame] | 25 | |
| 26 | echo 'Pattern: ' 'double backslash and star: "replace backslash and everything after it"' |
Denys Vlasenko | f56fe82 | 2010-08-06 17:21:52 +0200 | [diff] [blame] | 27 | echo 'Unquoted: ' ${v/\\*/_\\_\z_} |
Denys Vlasenko | e3354bc | 2016-10-02 18:08:04 +0200 | [diff] [blame] | 28 | r=${v/\\*/_\\_\z_} |
| 29 | echo 'Unquoted =: ' "$r" |
Denys Vlasenko | f56fe82 | 2010-08-06 17:21:52 +0200 | [diff] [blame] | 30 | echo 'Quoted: ' "${v/\\*/_\\_\z_}" |
Denys Vlasenko | e3354bc | 2016-10-02 18:08:04 +0200 | [diff] [blame] | 31 | r="${v/\\*/_\\_\z_}" |
| 32 | echo 'Quoted =: ' "$r" |
Denys Vlasenko | f56fe82 | 2010-08-06 17:21:52 +0200 | [diff] [blame] | 33 | |
| 34 | echo |
| 35 | |
| 36 | v='a\bc' |
| 37 | echo 'Source: ' "$v" |
| 38 | echo 'Replace str: ' '_\\_\z_' |
| 39 | |
Denys Vlasenko | 33bbb27 | 2010-08-07 22:24:36 +0200 | [diff] [blame] | 40 | echo 'Pattern: ' 'single backslash and b: "replace b"' |
Denys Vlasenko | f56fe82 | 2010-08-06 17:21:52 +0200 | [diff] [blame] | 41 | echo 'Unquoted: ' ${v/\b/_\\_\z_} |
Denys Vlasenko | e3354bc | 2016-10-02 18:08:04 +0200 | [diff] [blame] | 42 | r=${v/\b/_\\_\z_} |
| 43 | echo 'Unquoted =: ' "$r" |
Denys Vlasenko | f56fe82 | 2010-08-06 17:21:52 +0200 | [diff] [blame] | 44 | echo 'Quoted: ' "${v/\b/_\\_\z_}" |
Denys Vlasenko | e3354bc | 2016-10-02 18:08:04 +0200 | [diff] [blame] | 45 | r="${v/\b/_\\_\z_}" |
| 46 | echo 'Quoted =: ' "$r" |
Denys Vlasenko | f56fe82 | 2010-08-06 17:21:52 +0200 | [diff] [blame] | 47 | |
| 48 | echo 'Pattern: ' 'double backslash and b: "replace backslash and b"' |
Denys Vlasenko | f56fe82 | 2010-08-06 17:21:52 +0200 | [diff] [blame] | 49 | echo 'Unquoted: ' ${v/\\b/_\\_\z_} |
Denys Vlasenko | e3354bc | 2016-10-02 18:08:04 +0200 | [diff] [blame] | 50 | r=${v/\\b/_\\_\z_} |
| 51 | echo 'Unquoted =: ' "$r" |
Denys Vlasenko | f56fe82 | 2010-08-06 17:21:52 +0200 | [diff] [blame] | 52 | echo 'Quoted: ' "${v/\\b/_\\_\z_}" |
Denys Vlasenko | e3354bc | 2016-10-02 18:08:04 +0200 | [diff] [blame] | 53 | r="${v/\\b/_\\_\z_}" |
| 54 | echo 'Quoted =: ' "$r" |
| 55 | |
| 56 | echo |
| 57 | |
| 58 | v='a\bc' |
| 59 | s='_\\_\z_' |
| 60 | echo 'Source: ' "$v" |
| 61 | echo 'Replace str: ' "$s" '(as variable $s)' |
| 62 | |
| 63 | echo 'Pattern: ' 'single backslash and b: "replace b"' |
| 64 | echo 'Unquoted: ' ${v/\b/$s} |
| 65 | r=${v/\b/$s} |
| 66 | echo 'Unquoted =: ' "$r" |
| 67 | echo 'Quoted: ' "${v/\b/$s}" |
| 68 | r="${v/\b/$s}" |
| 69 | echo 'Quoted =: ' "$r" |
| 70 | |
| 71 | echo 'Pattern: ' 'double backslash and b: "replace backslash and b"' |
| 72 | echo 'Unquoted: ' ${v/\\b/$s} |
| 73 | r=${v/\\b/$s} |
| 74 | echo 'Unquoted =: ' "$r" |
| 75 | echo 'Quoted: ' "${v/\\b/$s}" |
| 76 | r="${v/\\b/$s}" |
| 77 | echo 'Quoted =: ' "$r" |
Denys Vlasenko | f56fe82 | 2010-08-06 17:21:52 +0200 | [diff] [blame] | 78 | |
| 79 | echo |
| 80 | |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 81 | echo Done: $? |