Search code examples
bashshellcross-platformgetopt

Cross-platform getopt for a shell script


I've just found out that getopt is not cross-platform (in particular for FreeBSD and Linux). What is the best workaround for this issue?


Solution

  • Use getopts (with an "s").

    According to Bash FAQ 35:

    Unless it's the version from util-linux, and you use its advanced mode, never use getopt(1). getopt cannot handle empty arguments strings, or arguments with embedded whitespace. Please forget that it ever existed.

    The POSIX shell (and others) offer getopts which is safe to use instead.