getopts

Small getopts tutorial

(via wiki.bash-hackers.org)

When you want to parse commandline arguments in a professional way, getopts is the tool of choice. Unlike its older brother getopt (note the missing s!), it's a shell builtin command. The advantage is

* you don't need to hand your positional parameters through to an external program
* getopts can easily set shell variables you can use for parsing (impossible for an external process!)
* you don't have to argue with several getopt implementations which had buggy concepts in the past (whitespaces, ...)
* getopts is defined in POSIX®

Syndicate content
Comment