The Linux foundation list of standard utilities includes getopts
but not getopt
. Similar for the Open Group list of Posix utilities.
Meanwhile, Wikipedia's list of standard Unix Commands includes getopt
but not getopts
. Similarly, the Windows Subsystem for Linux (based on Ubuntu based on Debian) also includes getopt
but not getopts
(and it is the GNU Enhanced version).
balter@spectre:~$ which getopt
/usr/bin/getopt
balter@spectre:~$ getopt -V
getopt from util-linux 2.27.1
balter@spectre:~$ which getopts
balter@spectre:~$
So if I want to pick one that I can be the most confident that anyone using one of the more standard Linux distros (e.g. Debian, Red Hat, Ubuntu, Fedora, CentOS, etc.), which should I pick?
Note:
thanks to Michael and Muru for explaining about builtin vs executable. I had just stumbled across this as well which lists bash builtins.
getopts
is a shell built-in (as both of your sources identify), not an executable. It's in every POSIX shell, including the one from WSL Ubuntu. What are you actually trying to do with it? It's not impossible that there are cases where it matters which you use, but there's not enough information here to say. – Michael Homer May 14 '18 at 04:54getopt
orgetopts
at all is a warning sign that your shell script is probably complicated enough that you should consider rewriting it in a better programming language, e.g. Perl, Python, Ruby, even PHP. – zwol May 14 '18 at 15:44