Yes, the optional args
arguments are a way to pass custom parameters. As it says in the manual:
getopts normally parses the positional parameters, but if more arguments are given in args, getopts parses those instead.
So if you have an array:
myargs=(arg1 arg2 arg3)
you can use:
getopts optstring name "${myargs[@]}"
to process them.
Example:
$ # Set positional parameters:
$ set -- -a 1 -b
$ getopts a:b var -a 2
$ echo $var
a
$ echo $OPTARG
2
As you can see, it retrieved the value from the args I supplied to the command (2
), not the positional parameters to the shell (1
)