variable

OPTIND

Index of next argument to process; initialized to 1 at shell invocation. Reset to 1 when beginning a new parse. After the loop, shift OPTIND-1 arguments to leave operands.

Formal Syntax & Structure

bash syntaxbash
OPTIND=1; getopts optstring name

Return Value

integer

Index of next argument to process; initialized to 1 at shell invocation. Reset to 1 when beginning a new parse. After the loop, shift OPTIND-1 arguments to leave operands.

Working Example

Example Snippetbash
parse() {    local OPTIND=1 OPTARG opt verbose=0 name=guest    while getopts ':vn:' opt; do        case $opt in            v) verbose=1 ;;            n) name=$OPTARG ;;            \?) printf 'unknown: %s\n' "$OPTARG" >&2; return 2 ;;            :) printf 'missing: %s\n' "$OPTARG" >&2; return 2 ;;        esac    done    shift "$((OPTIND - 1))"    printf 'verbose=%s name=[%s] operands=%s\n' "$verbose" "$name" "$#"}parse -vn 'Ada Lovelace' -- fileparse -n first -n last
Bash Reference