Which is the more standard package, getopt or getopts (with an "s")?
which
is the wrong tool. getopts
is usually also a builtin:
Since
getopts
affects the current shell execution environment, it is generally provided as a shell regular built-in.
~ for sh in dash ksh bash zsh; do "$sh" -c 'printf "%s in %s\n" "$(type getopts)" "$0"'; done
getopts is a shell builtin in dash
getopts is a shell builtin in ksh
getopts is a shell builtin in bash
getopts is a shell builtin in zsh
If you're using a shell script, you can safely depend on getopts
. There might be other reasons to favour one or the other, but getopts
is standard.
See also: Why not use "which"? What to use then?
I too would prefer getopts
over getopt
for the following reasons:
getopt
Cons
- External utility
- Cannot handle empty argument string or arguments with embedded whitespace in traditional version
getopts
Pros
- Works in any POSIX shell and is portable
- Works well with
-a -b
as well as-ab