I want to parse some arguments to a bash script using getopts but want to be able to access the remaining args that are not included in the option list. So for example, if I have a call:

% script -a -b param -c param -d other arguments here

I would have:

while getopts "ab:c:d" opt ; do
.
done

What is the easiest way to get "other arguments here", which should be unprocessed by getopts?

link|improve this question

80% accept rate
feedback

1 Answer

up vote 1 down vote accepted

you need to shift when you parse an arg, or put

shift $((OPTIND -1)) after you have finished parsing, then deal in the usual way e.g.

while getopts "ab:c:d" opt ; do
.
done
shift $(( $OPTIND -1 ))

while test $# -gt 0; do
echo $1
shift
done

the $(( )) is not available in all shells, for portability, you may want to use expr instead.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.