Skip to content

Instantly share code, notes, and snippets.

@thealanberman
Created March 17, 2019 06:33
Show Gist options
  • Save thealanberman/7534842d0d1388211b2c0f7e505cf240 to your computer and use it in GitHub Desktop.
Save thealanberman/7534842d0d1388211b2c0f7e505cf240 to your computer and use it in GitHub Desktop.
Self documenting Bash script
#!/usr/bin/env bash
if [[ ${*} =~ help|-h || ${#} == 0 ]]; then cat <<HELP_USAGE | sed "s/^#? //"
#? USAGE
#? ======
#? Anywhere in your script:
#?
#? if [[ $* =~ help|-h || $# == 0 ]]; then cat <<HELP_USAGE | sed "s/^#? //"
#? #? Documentation lines prefixed with '#? '
#? HELP_USAGE
#? exit 0; fi
#?
#? FEATURES
#? ========
#? * Preformatted.
#? * Variable expansion: \${HOME} is '${HOME}'
#? * Commands can be executed
#? '\$(echo Foo)' results in '$(echo Foo)'
#? * Script being documented can self referenced
#? '\${0}' => '${0}'
#?
HELP_USAGE
exit 0; fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment