learned-form-shellcheck.sh 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #! /bin/bash
  2. # Examples of fixing problems ponted out by shellcheck
  3. # These are things learned from running shellcheck on my bash
  4. # https://github.com/koalaman/shellcheck
  5. # https://www.shellcheck.net/wiki/Directive
  6. # Principals:
  7. # - Favor readability (e.g. over POSIX compliance)
  8. # - Don't define anything when a built-in works
  9. # - Use any automathic type/variable/safety/syntax check availabe
  10. # Favor readability over POSIX compliance
  11. #
  12. # shellcheck disable=SC3046 # "source" is more readable than "."
  13. # shellcheck disable=SC2112 # "function" is more readable than "()"
  14. # This is bash, be safe
  15. set -u
  16. set -e
  17. # BAD, old
  18. # source ~/lib/bash/bashutils.sh || `: # Use my library if available`\
  19. # `: fall back to echo`
  20. # alias info=echo && \
  21. # alias warn=echo && \
  22. # alias error=echo
  23. #
  24. # GOOD, new, fixed
  25. # shellcheck disable=SC1090 # Clean up seperatly
  26. source ~/lib/bash/bashutils.sh || : Use my library if available \
  27. : else fall back to echo && \
  28. function info() { echo "$@"; } && \
  29. function warn() { echo "$@"; } && \
  30. function error() { echo "$@"; } && \
  31. function announce() { echo "$@"; }
  32. announce defined alternatives