It seems to getoptsjust exit the loop when some unknown optional argument ( abc) is found.
I found the following workaround by wrapping the loop getoptsin another loop:
#!/usr/bin/env bash
while :; do
while getopts :f arg; do
case $arg in
f)
echo Option $arg specified.
;;
*)
echo Unknown option: $OPTARG.
;;
esac
done
((OPTIND++))
[ $OPTIND -gt $# ] && break
done
Then skip the invalid arguments and break the loop when the maximum arguments are reached.
Output:
$ ./test.sh abc -f
Option f specified.
$ ./test.sh -a abc -f
Unknown option: a.
Option f specified.