How can I make bash deal with long param using "getopt" command in mac?

qiushuitian picture qiushuitian · Aug 28, 2012 · Viewed 15.7k times · Source

I want to make my bash script deal with long parameters. I found getopt, but it isn't supported in OS X. Can anyone tell me why getopt was implemented by BSD, but not GNU? I tried building getopt in GNU C lib, but it failed for my poor skills with Linux.

Did anyone do this work?

Answer

Denis Rouzaud picture Denis Rouzaud · May 27, 2016

There is a brew bottle for getopt.

Just run brew install gnu-getopt.

You can either specify the path for it like /usr/local/Cellar/gnu-getopt/1.1.6/bin/getopt

Or use brew link --force gnu-getopt so it will be linked in /usr/local/bin/

Just be aware that forcing linking might be corrupting your system (as it replaces the system getopt by the gnu one).

See also other answer suggesting to define FLAGS_GETOPT_CMD.