All right, figured it out:
After rifling through the man page for getopts
, I found this tidbit (emphasis mine):
Each time it is invoked, getopts places...the index of the next argument to be processed into the variable OPTIND. OPTIND is initialized to 1 each time the shell or a shell script is invoked.... The shell does not reset OPTIND automatically; it must be manually reset between multiple calls to getopts within the same shell invocation if a new set of parameters is to be used.
Since I only run the script once from by .bashrc, OPTIND
only gets initialized once. The first time I run the function, everything's hunky dory. The second time on, OPTIND
is set to 2 and getopts
doesn't find anything there, so it moves on.
Armed with this knowledge, I modified upd()
to reset OPTIND
to 1:
upd() {
__set_dirs
if [[ -n ${stgdir} ]]
then
__overwrite=0
OPTIND=1
while getopts "o" opt
...
That fixed it. OPTIND
: more important than you'd think.