I saw a getopts usage like this:
while getopts ":hv-:" optchar; do
...
done
What is the use of the last -:, with this placement, what the - and : have to do with the parsing behavior?
I saw a getopts usage like this:
while getopts ":hv-:" optchar; do
...
done
What is the use of the last -:, with this placement, what the - and : have to do with the parsing behavior?
Copyright © 2021 Jogjafile Inc.
The
:-:at the end of the optstring(":hv-:")is used to handle long options (options starting with --). The placement of-and:after thevdoes not directly impact the behavior of thevoption.Here's the breakdown:
The getopts command uses the
--to distinguish between short and long options. When a long option is encountered, it is handled in the--) case inside the getopts loop. The colon(:)after the-in:-:is associated with long options that require arguments. For example, if you have a long option like--input=file.txt, the:after-allows you to specify that input requires an argument.Here's a brief example:
In this example, short options
-hand-vare handled, and long options--hand--vare also handled. The:-:in the optstring enables the handling of long options and their potential arguments.