I'm writing a bash script, parsing options with getopts like this:
#!/bin/bash
while getopts ab: ; do
case $opt in
a) AOPT=1
;;
b) BOPT=$OPTARG
;;
esac
done
I'd like to have the "-b" option OPTIONALLY take an argument, but as it is, getopts complains if no argument is passed. How can I accomplish this?
Thanks!
You can run getopts in silent mode by including a colon as the first character of the optstring. This can be used to suppress the error message.
From the getopts manpage:
Thus something like the following might work for you:
Some examples: