0

I wrote a bash script, how can i add a long options for example: --verbose along with -v option?

My second question is what is the difference between while getopts :v and while getopts v, i've done some research and i still can't fully understand it.

I have tried it with getopts but not working, also heard that getopt is not the right choice.

while getopts ":v" option; do
  case $option in
    v) verbose=true;;
    ?) echo "Invalid option: -$OPTARG" >&2 && exit 1;;
  esac
done
  • https://linux.die.net/man/1/getopt supports long options – Diego Torres Milano Jul 15 '23 at 04:49
  • 1
    @DiegoTorresMilano the GNU version of `getopt` supports long options, but other versions don't, making it a compatibility nightmare. See [Complex Option Parsing on Greycat's BashFAQ](http://mywiki.wooledge.org/ComplexOptionParsing). – Gordon Davisson Jul 15 '23 at 05:12
  • 1
    Do the answers to ["How do I parse command line arguments in Bash?"](https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash) and [BashFAQ #35: "How can I handle command-line options and arguments in my script easily?"](http://mywiki.wooledge.org/BashFAQ/035) answer your question? – Gordon Davisson Jul 15 '23 at 05:13
  • which getopt variant are you using? – phuclv Jul 15 '23 at 10:38

0 Answers0