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 |
| 9 | # unquoted and assignment expansions - they have a backslash before z. |
Denys Vlasenko | f56fe82 | 2010-08-06 17:21:52 +0200 | [diff] [blame] | 10 | |
| 11 | v='a*b\*c' |
| 12 | echo 'Source: ' "$v" |
| 13 | echo 'Replace str: ' '_\\_\z_' |
| 14 | |
| 15 | echo 'Pattern: ' 'single backslash and star: "replace literal star"' |
| 16 | r=${v/\*/_\\_\z_} |
| 17 | echo 'In assignment:' "$r" |
| 18 | echo 'Unquoted: ' ${v/\*/_\\_\z_} |
| 19 | echo 'Quoted: ' "${v/\*/_\\_\z_}" |
| 20 | |
| 21 | echo 'Pattern: ' 'double backslash and star: "replace backslash and everything after it"' |
| 22 | r=${v/\\*/_\\_\z_} |
| 23 | echo 'In assignment:' "$r" |
| 24 | echo 'Unquoted: ' ${v/\\*/_\\_\z_} |
| 25 | echo 'Quoted: ' "${v/\\*/_\\_\z_}" |
| 26 | |
| 27 | echo |
| 28 | |
| 29 | v='a\bc' |
| 30 | echo 'Source: ' "$v" |
| 31 | echo 'Replace str: ' '_\\_\z_' |
| 32 | |
| 33 | echo 'Pattern: ' 'single backslash and b: "replace literal b"' |
| 34 | r=${v/\b/_\\_\z_} |
| 35 | echo 'In assignment:' "$r" |
| 36 | echo 'Unquoted: ' ${v/\b/_\\_\z_} |
| 37 | echo 'Quoted: ' "${v/\b/_\\_\z_}" |
| 38 | |
| 39 | echo 'Pattern: ' 'double backslash and b: "replace backslash and b"' |
| 40 | r=${v/\\b/_\\_\z_} |
| 41 | echo 'In assignment:' "$r" |
| 42 | echo 'Unquoted: ' ${v/\\b/_\\_\z_} |
| 43 | echo 'Quoted: ' "${v/\\b/_\\_\z_}" |
| 44 | |
| 45 | echo |
| 46 | |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 47 | echo Done: $? |