Show pageOld revisionsBacklinksBack to top This page is read only. You can view the source, but not change it. Ask your administrator if you think this is wrong. ===== Overview ===== An attempt at summarizing the //right// way to use POSIX-compliant ''getopts''. A couple links: * [[https://pubs.opengroup.org/onlinepubs/9699919799/utilities/getopts.html|https://pubs.opengroup.org/onlinepubs/9699919799/utilities/getopts.html]] * [[https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html#tag_12_02|https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html#tag_12_02]] * [[https://sookocheff.com/post/bash/parsing-bash-script-arguments-with-shopts/|https://sookocheff.com/post/bash/parsing-bash-script-arguments-with-shopts/]] ===== Starting points ===== First, there has been much blood spilled discussing how to support //optional// arguments using ''getopts'' -- the correct solution is to not do that, as explained in Guideline 7 [[https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html#tag_12_02|here]]. So we won't. And, second, if the //optstring// handed to getopts starts with a colon, then getopts uses what is called "silent error reporting"; this appears to be the recommended approach, so we'll stick with that. In short, don't do this: <code> while getopts ab:c: opt ; do </code> Do this (note the leading colon): <code> while getopts :ab:c: opt ; do </code> Explanation below. ===== Fleshed out example ===== <code> ... coming soon ... </code> posix_shell_getpots.txt Last modified: 2019/09/30 12:16by rpjday