bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Error when using getopts in a function


From: Chet Ramey
Subject: Re: Error when using getopts in a function
Date: Wed, 27 Dec 2000 11:56:27 -0500

> Machine Type: sparc-sun-solaris2.6
> 
> Bash Version: 2.03
> Patch Level: 0
> Release Status: release
> 
> Description:
>       The getopts shell builtin uses the OPTIND variable to keep
>       track of the next argument to be processed. When all the
>       options have been processed, OPTIND is generally used to
>       shift the options out of the argument list. Bash does not
>       reset OPTIND when a function exits, so calling the same
>       function multiple times will result in the argument list
>       being parsed incorrectly (e.g. options are skipped or
>       arguments are incorrectly shifted out of the argument list).

And documents the behavior, which is required by POSIX.  Functions and
their callers share the same variables.

The manual page says

        The shell does not reset
        .SM
        .B OPTIND
        automatically; it must be manually reset between multiple
        calls to
        .B getopts
        within the same shell invocation if a new set of parameters
        is to be used.

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
( ``Discere est Dolere'' -- chet)

Chet Ramey, CWRU    address@hidden    http://cnswww.cns.cwru.edu/~chet/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]