Linux getopt Command
Categories:
What is Linux getopt Command?
In this tutorial we learn how to use getopt command in Linux. getopt command is getopt - parse command options (enhanced)
getopt Syntax
getopt optstring parameters
getopt [options] [--] optstring parameters
getopt [options] -o|--options optstring [options] [--] parameters
The parameters getopt is called with can be divided into two parts: options which modify the way getopt will do the parsing (the options and the optstring in the SYNOPSIS), and the parameters which
If the environment variable GETOPT_COMPATIBLE is set, or if the first parameter is not an option (does not start with a '-', the first format in the SYNOPSIS), getopt will generate output that is
compatible with that of other versions of getopt(1). It will still do parameter shuffling and recognize optional arguments (see section COMPATIBILITY for more information).
Traditional implementations of getopt(1) are unable to cope with whitespace and other (shell-specific) special characters in arguments and non-option parameters. To solve this problem, this imple‐
getopt in a way that is no longer compatible with other versions (the second or third format in the SYNOPSIS). To determine whether this enhanced version of getopt(1) is installed, a special test
The name that will be used by the getopt(3) routines when it reports errors. Note that errors of getopt(1) are still reported as coming from getopt.
The short (one-character) options to be recognized. If this option is not found, the first parameter of getopt that does not start with a '-' (and is not an option argument) is used as the
Disable error reporting by getopt(3).
Do not generate normal output. Errors are still reported by getopt(3), unless you also use -q.
Test if your getopt(1) is this enhanced version or an old version. This generates no output, and sets the error status to 4. Other implementations of getopt(1), and this version if the en‐
Do not quote the output. Note that whitespace and special (shell-dependent) characters can cause havoc in this mode (like they do with other getopt(1) implementations).
This section specifies the format of the second part of the parameters of getopt (the parameters in the SYNOPSIS). The next section (OUTPUT) describes the output that is generated. These parame‐
list of getopt (see the EXAMPLES). All parsing is done by the GNU getopt(3) routines.
getopt(1) implementations do not support optional arguments.
This version of getopt(1) is written to be as compatible as possible to other versions. Usually you can just replace them with this version without any modifications, and with some advantages.
If the first character of the first parameter of getopt is not a '-', getopt goes into compatibility mode. It will interpret its first parameter as the string of short options, and all other argu‐
The environment variable GETOPT_COMPATIBLE forces getopt into compatibility mode. Setting both this environment variable and POSIXLY_CORRECT offers 100% compatibility for 'difficult' programs.
getopt returns error code 0 for successful parsing, 1 if getopt(3) returns errors, 2 if it does not understand its own parameters, 3 if an internal error occurs like out-of-memory, and 4 if it is
Example scripts for (ba)sh and (t)csh are provided with the getopt(1) distribution, and are optionally installed in /usr/share/getopt/ or /usr/share/doc/ in the util-linux subdirectory.
This environment variable is examined by the getopt(3) routines. If it is set, parsing stops as soon as a parameter is found that is not an option or an option argument. All remaining pa‐
Forces getopt to use the first calling format as specified in the SYNOPSIS.
getopt(3) can parse long options with optional arguments that are given an empty optional argument (but cannot do this for short options). This getopt(1) treats optional arguments that are empty as
bash(1), tcsh(1), getopt(3)
The getopt command is part of the util-linux package and is available from Linux Kernel Archive ⟨https://www.kernel.org/pub/linux/utils/util-linux/⟩.
getopt Examples
getopt Command Manual / Help
We can use man
and info
command to see the manual page of getopt command.
getopt command also have --help
option to show list of options.
To open man page for getopt command we can use command below. To exit man or info page you can press q
.
man getopt
To open info page for getopt command we can use command below.
info getopt
To open help page from getopt command we can run command below.
getopt --help
Linux getopt Command Source Code
You can find getopt command source code from the folowing repositories:
Related Linux Commands
You can read tutorials of related Linux commands below:
Summary
In this tutorial we learn how to use getopt in Linux with practical examples. Visit our Linux Commands guide to learn more about using command line interface in Linux.