The most common used idiom in Unix to separate the command line flags from the rest of the arguments is to use a double dash (--
).
If the utility uses the C library function getopt()
(either directly or indirectly), the double dash will signal the end of the command line options, and getopt()
will end its parsing of the command line.
This is from the getopt(3)
manual on OpenBSD:
The interpretation of options in the argument list may be cancelled by the option ‘--’ (double dash) which causes getopt() to signal the end of argument processing and return -1.
This is why the correct way to delete a file called -f
is
$ rm -- -f
for example.
It is likely that your shell implements the built-in getopts
command with a call to this library function.
IMHO, it's nice to know how to use getopts
in shell scripts, and getopt()
in C programs. It's not very difficult, and it makes life a lot easier when you write the command line parsing code of your project.
There is also a getopt_long()
available in some C libraries, for parsing "long options" (--option=value
). This, however, is an extension (no pun intended).