var_bash4.tests 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # 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 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.
  13. v='a*b\*c'
  14. echo 'Source: ' "$v"
  15. echo 'Replace str: ' '_\\_\z_'
  16. echo 'Pattern: ' 'single backslash and star: "replace literal star"'
  17. echo 'Unquoted: ' ${v/\*/_\\_\z_}
  18. r=${v/\*/_\\_\z_}
  19. echo 'Unquoted =: ' "$r"
  20. echo 'Quoted: ' "${v/\*/_\\_\z_}"
  21. r="${v/\*/_\\_\z_}"
  22. echo 'Quoted =: ' "$r"
  23. echo 'Pattern: ' 'double backslash and star: "replace backslash and everything after it"'
  24. echo 'Unquoted: ' ${v/\\*/_\\_\z_}
  25. r=${v/\\*/_\\_\z_}
  26. echo 'Unquoted =: ' "$r"
  27. echo 'Quoted: ' "${v/\\*/_\\_\z_}"
  28. r="${v/\\*/_\\_\z_}"
  29. echo 'Quoted =: ' "$r"
  30. echo
  31. v='a\bc'
  32. echo 'Source: ' "$v"
  33. echo 'Replace str: ' '_\\_\z_'
  34. echo 'Pattern: ' 'single backslash and b: "replace b"'
  35. echo 'Unquoted: ' ${v/\b/_\\_\z_}
  36. r=${v/\b/_\\_\z_}
  37. echo 'Unquoted =: ' "$r"
  38. echo 'Quoted: ' "${v/\b/_\\_\z_}"
  39. r="${v/\b/_\\_\z_}"
  40. echo 'Quoted =: ' "$r"
  41. echo 'Pattern: ' 'double backslash and b: "replace backslash and b"'
  42. echo 'Unquoted: ' ${v/\\b/_\\_\z_}
  43. r=${v/\\b/_\\_\z_}
  44. echo 'Unquoted =: ' "$r"
  45. echo 'Quoted: ' "${v/\\b/_\\_\z_}"
  46. r="${v/\\b/_\\_\z_}"
  47. echo 'Quoted =: ' "$r"
  48. echo
  49. v='a\bc'
  50. s='_\\_\z_'
  51. echo 'Source: ' "$v"
  52. echo 'Replace str: ' "$s" '(as variable $s)'
  53. echo 'Pattern: ' 'single backslash and b: "replace b"'
  54. echo 'Unquoted: ' ${v/\b/$s}
  55. r=${v/\b/$s}
  56. echo 'Unquoted =: ' "$r"
  57. echo 'Quoted: ' "${v/\b/$s}"
  58. r="${v/\b/$s}"
  59. echo 'Quoted =: ' "$r"
  60. echo 'Pattern: ' 'double backslash and b: "replace backslash and b"'
  61. echo 'Unquoted: ' ${v/\\b/$s}
  62. r=${v/\\b/$s}
  63. echo 'Unquoted =: ' "$r"
  64. echo 'Quoted: ' "${v/\\b/$s}"
  65. r="${v/\\b/$s}"
  66. echo 'Quoted =: ' "$r"
  67. echo
  68. echo Done: $?