/bash-resources

Resources to help me write better Bash scripts

Primary LanguageShell

Bash resources

Some resources to help me write good (or at least better) Bash scripts.

Please see the accompanying template.

  • Template - Template for starting new scripts.
  • Snippets - Reusable snippets.
  • Scripts - A collection of full scripts.

Tips

  • Use a recent version of Bash.
    • MacOS (at the time of writing) uses an old version of Bash by default (3.2). Upgrade with e.g. brew install bash.

Writing scripts

Tips for writing scripts, as opposed to interactive command line use.

  • Use ShellCheck.
  • Always prefer long form parameters (e.g. --long-form) over short form (e.g. -l).
    • This makes scripts much more readable.
  • Prefix variables with local inside functions.
    • Variables are globally scoped by default. This reduces the scope to just the function.

Tricks

Resources