Bash - Show help function template

By xngo on June 29, 2019

When users execute an invalid command option on your Bash script, it is useful to show users the help informations. Below is a template that you can use to show the helf informations.

show_help() {
cat << EOF
Usage: ${0##*/} [-hv] [-f OUTFILE] [FILE]...
Do stuff with FILE and write the result to standard output. With no FILE
or when FILE is -, read standard input.
 
  -h          display this help and exit
  -f OUTFILE  write the result to OUTFILE instead of standard output.
  -v          verbose mode. Can be used multiple times for increased
              verbosity.
EOF
}

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.