Can I use getopts for parsing command-line arguments in my bash scripts(not sh) without worrying about another depedency?

2 Answers 2

$ type getopts
getopts is a shell builtin

getopts is built in, so it's present anywhere bash is.

To be sure of that you'd need to check the bash documentation to see whether it's an optional feature — it isn't.

More precisely, this tells you that getopts is available in this particular version of bash. You need to check the release notes for older versions to see when getopts was introduced; that happened before the dawn of time (it was already present in bash 1.14 and you'd have a hard time finding a version that old).

Even better, you can count on getopts even in shells that aren't bash, since it's required by POSIX.

  • Note that getopt is not the same as getopts. getopt is a standalone program. Feb 24, 2017 at 16:39

Yes, check man getopts. Even though getopts is a program in /usr/bin You can see that it redirects you to a list of builtin bash commands BASH_BUILTINS(1) and that the bash documentation specifically refers to getopts.

    getopts optstring name [args]

For more detailed usage of how to use getopts see "example of how to use getopts in bash"

  • 1
    Woud you mind detailing more the answer, man page quotation, how to use? There is nothing against you answering you own posts, however I would advise you to write answers of better quality. Jul 6, 2016 at 15:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .