LinuxCommandLibrary
GitHubF-DroidGoogle Play Store

getopt

command-line option parser for shell scripts

TLDR

Parse optional verbose/version flags with shorthands
$ getopt -o vV -l verbose,version -- --version --verbose
copy
Add a --file option with a required argument
$ getopt -o f: -l file: -- --file=somefile
copy
Add a --verbose option with an optional argument and pass a non-option parameter
$ getopt -o v:: -l verbose:: -- --verbose arg
copy
Parse multiple options with various argument requirements
$ getopt -o rv::t: -l verbose,source::,target: -- -v --target target
copy

SYNOPSIS

getopt [options] [--] parameters

DESCRIPTION

getopt parses command-line options for shell scripts, handling both short (-v) and long (--verbose) option formats. It normalizes option order, handles bundled short options (-abc), and separates options from arguments.The enhanced getopt (util-linux version) supports long options, optional arguments, and proper handling of arguments with spaces. Use getopt -T to test if the enhanced version is available.Output is typically evaluated with eval set -- to update the script's positional parameters with the normalized options.

PARAMETERS

-o, --options SHORT

Short options to recognize (single letters). Colon after letter means required argument, double colon means optional.
-l, --longoptions LONG
Long options to recognize (comma-separated). Colon rules same as short options.
-n, --name NAME
Name to use in error messages
-q, --quiet
Suppress error messages
-a, --alternative
Allow long options with single dash
-T, --test
Test for enhanced getopt

OPTION ARGUMENT SYNTAX

In the options string:- f - flag with no argument- f: - option with required argument- f:: - option with optional argument

INSTALL

sudo apt install util-linux
copy
sudo dnf install util-linux
copy
sudo pacman -S util-linux
copy
sudo apk add util-linux-misc
copy
sudo zypper install util-linux
copy
brew install util-linux
copy
nix profile install nixpkgs#util-linux
copy

CAVEATS

The original BSD getopt is limited and doesn't support long options. Always test for enhanced getopt with -T. Optional arguments must be attached directly (--opt=arg, not --opt arg).

HISTORY

getopt dates back to early Unix systems. The enhanced version in util-linux was developed to overcome limitations of the original, adding long option support and proper quoting for arguments with special characters.

SEE ALSO

getopts(1), bash(1)

RESOURCES

Braincup
Open source brain training for math, memory and focus
Braincup mini-games
41 mini-games · Apache-2.0
No ads · No tracking
Play in browser
Download Braincup on the App StoreGet Braincup on Google PlayGet Braincup on F-Droid
276 stars
From the maker of Linux Command Library
Copied to clipboard
Braincup
Open source brain training for math, memory and focus. 41 mini-games, from mental arithmetic to Sudoku, N-Back and Solo Chess.
Apache-2.0 licensed · No ads · No tracking · No account
From the maker of Linux Command Library
Download Braincup on the App StoreGet Braincup on Google PlayGet Braincup on F-Droid