parse arguments after getopts

speendo picture speendo · Feb 27, 2012 · Viewed 19.3k times · Source

I want to call a bash script like this

$ ./scriptName -o -p -t something path/to/file

This is as far as I get

#!/bin/bash

o=false
p=false

while getopts ":opt:" options
do
    case $options in
        o ) opt1=true
        ;;
        p ) opt2=true
        ;;
        t ) opt3=$OPTARG
        ;;
    esac
done

but how do I get the path/to/file?

Answer

Oliver Charlesworth picture Oliver Charlesworth · Feb 27, 2012

You can do something like:

shift $(($OPTIND - 1))
first_arg=$1
second_arg=$2

after the loop has run.