sed-in-place 700 B

1234567891011121314151617181920212223242526
  1. #!/usr/bin/env bash
  2. set -eEuo pipefail
  3. # sed is NOT portable across OSes
  4. # sed -i '' does in-place on Mac, BSD, and other POSIX-compliant OSes
  5. # sed -i '' does not work with GNU sed, but sed -i (without small quotes) does
  6. # assume that sed is not GNU sed initially
  7. SED=(sed -i '')
  8. if sed --help &>/dev/null; then
  9. # if sed doesn't have help text, it isn't GNU sed
  10. if [[ $(sed --help 2>&1) == *GNU* ]]; then
  11. SED=(sed -i)
  12. fi
  13. fi
  14. # sed -e is required on Mac/BSD if the -i option is used
  15. # sed -e is not required but is supported by GNU sed
  16. # Therefore, this script supplies -e it unless the first argument to this script is a flag
  17. if [[ $1 != -* ]]; then
  18. SED+=(-e)
  19. fi
  20. "${SED[@]}" "${@}"