Getopts
Here's a useful pattern for collecting command line options in a script:
I like to combine this with a check for superfluous positional arguemnts:
If you only expect a specific number of positional arguments, this is a good safety measure, because it reduces the risk of missing important options. Because getopts
will stop processing options as soon as it hits a positional argument, in a case like this:
the -h
flag will be ignored. Imagine it being a dry-run toggle, for example - better to quit than to miss that.
Last updated