Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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

share|improve this question
up vote 9 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
shift $(expr $OPTIND - 1 )

while test $# -gt 0; do
  echo $1
share|improve this answer

Your Answer


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.