Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer 1

up vote 3 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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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