Skip to content

Instantly share code, notes, and snippets.

@RichardBronosky
Created December 2, 2019 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RichardBronosky/4dd229c9df4dd2d6b9e9337bade0c2f2 to your computer and use it in GitHub Desktop.
Save RichardBronosky/4dd229c9df4dd2d6b9e9337bade0c2f2 to your computer and use it in GitHub Desktop.

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