var_bash4.tests 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 and assignment expansions - they have a backslash before z.
  10. v='a*b\*c'
  11. echo 'Source: ' "$v"
  12. echo 'Replace str: ' '_\\_\z_'
  13. echo 'Pattern: ' 'single backslash and star: "replace literal star"'
  14. r=${v/\*/_\\_\z_}
  15. echo 'In assignment:' "$r"
  16. echo 'Unquoted: ' ${v/\*/_\\_\z_}
  17. echo 'Quoted: ' "${v/\*/_\\_\z_}"
  18. echo 'Pattern: ' 'double backslash and star: "replace backslash and everything after it"'
  19. r=${v/\\*/_\\_\z_}
  20. echo 'In assignment:' "$r"
  21. echo 'Unquoted: ' ${v/\\*/_\\_\z_}
  22. echo 'Quoted: ' "${v/\\*/_\\_\z_}"
  23. echo
  24. v='a\bc'
  25. echo 'Source: ' "$v"
  26. echo 'Replace str: ' '_\\_\z_'
  27. echo 'Pattern: ' 'single backslash and b: "replace b"'
  28. r=${v/\b/_\\_\z_}
  29. echo 'In assignment:' "$r"
  30. echo 'Unquoted: ' ${v/\b/_\\_\z_}
  31. echo 'Quoted: ' "${v/\b/_\\_\z_}"
  32. echo 'Pattern: ' 'double backslash and b: "replace backslash and b"'
  33. r=${v/\\b/_\\_\z_}
  34. echo 'In assignment:' "$r"
  35. echo 'Unquoted: ' ${v/\\b/_\\_\z_}
  36. echo 'Quoted: ' "${v/\\b/_\\_\z_}"
  37. echo
  38. echo Done: $?