getopt -- parse command line options


#include <unistd.h>

int getopt(int argc, char * const argv[], const char * optstring);

extern char *optarg;
extern int optind, opterr, optopt;


The getopt() function shall parse command line arguments as described in ISO POSIX (2003), with the following exceptions, where LSB and POSIX specifications vary. LSB systems shall implement the modified behaviors described below.

Argument Ordering

The getopt() function can process command line arguments referenced by argv in one of three ways:


the order of arguments in argv is altered so that all options (and their arguments) are moved in front of all of the operands. This is the default behavior.

Note: This behavior has undefined results if argv is not modifiable. This is to support historic behavior predating the use of const and ISO C (1999). The function prototype was aligned with ISO POSIX (2003) despite the fact that it modifies argv, and the library maintainers are unwilling to change this.


The arguments in argv are processed in exactly the order given, and option processing stops when the first non-option argument is reached, or when the element of argv is "--". This ordering can be enforced either by setting the environment variable POSIXLY_CORRECT, or by setting the first character of optstring to '+'.


The order of arguments is not altered, and all arguments are processed. Non-option arguments (operands) are handled as if they were the argument to an option with the value 1 ('\001'). This ordering is selected by setting the first character of optstring to '-';

Option Characteristics

LSB specifies that:

POSIX specifies that:


LSB specifies that:

POSIX specifies that:

Return Values

LSB specifies the following additional getopt() return values:

Any other return value has the same meaning as for POSIX.

POSIX specifies the following getopt() return values:

Environment Variables

LSB specifies that: