Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

Shell Script Design Goals

  • Don't avoid bashisms unless you know bash will be unavailable
    • Use #!/bin/bash rather than #!/bin/sh
    • If you are going to try to avoid bash, you must test on a system that does not have bash
  • Always include a shebang #!
  • Handle errors
    • Use set -eu (errexit & nounset)
  • Sould be idemponent if read via source
    • Use subshells to avoid leaking unintended variables into the parent scope
    • Only define variables and functions
    • Don't run functions or commands that modify the system or environment
  • Namespace variables and functions
  • Mark internal variables and functions
    • Beginning a variable or function with an underscore is a common indicator of internal functionality
    • Beginning a variable or function with an underscore does not enforce internal-only
  • Related functionality should be placed in a single script and behave differently according to the name called
    • Having multiple symlinks to the same script is a convenient way to optionally avoid specifying functionality via argument
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.