I have this script:
#!/usr/bin/env bash
first_arg=$1
second_arg=$2
while getopts ":a:b" opt; do
case $opt in
a)
echo "-a found"
some_value=$OPTARG
;;
b)
echo "-b found"
;;
\?)
echo "Unknown flag"
esac
done
echo "$some_value"
When I call it like this: ./test.sh -a third
the getopts while loop works and the output is:
-a found
third
But when I call it with 2 additional parameters like this: ./test.sh first second -a third
the while loop doesn't find anything in getopts and the output is blank (i.e. because $some_value is empty). What is happening to getopts when I add more parameters to the shell script command and how do I fix it?