Skip to content

Instantly share code, notes, and snippets.

@paulscott
Created March 21, 2019 21:36
Show Gist options
  • Save paulscott/f489a342bf94e73fa9022238f5f341a0 to your computer and use it in GitHub Desktop.
Save paulscott/f489a342bf94e73fa9022238f5f341a0 to your computer and use it in GitHub Desktop.
A simple way to output command usage for shell scripts
#!/bin/bash
# A one line description of what the command does
#
# Usage:
# commandName
# what this command does with no arguments
# commandName --argument [--optionalArg=value]
# what this command does with these arguments. one of them is optional.
#
# options:
# --someOption='val' : This option can apply to any invoation of the command.
function usage {
usageEnd=$((LINENO - 2))
sed -n "2,${usageEnd}p;${usageEnd}q" "${0}" | sed 's/^#//'
}
# The usage function should be at the top of the file immediately after the usage comment.
# The sed one liner will output the comment and strip off the hash signs.
usage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment