I has an existing script which works on $1 $2 $3, etc. I want to add an optional argument without disturbing the order.
Do we have some ready made solution for: If a valid option (using getopts) is found, this index of argument should not be considered as argument.
The usual solution is to use getopts
to scan and process the optional flag options, and then execute the command:
shift $((OPTIND-1))
When getopts
finishes, $OPTIND
will be the number of the first as-yet-unprocessed argument. The shift
command then deletes the first OPTIND-1
arguments, which effectively renumbers the unhandled parameters to $1
, $2
,....