-
-
Notifications
You must be signed in to change notification settings - Fork 2
Linux getopt Guide
Mattscreative edited this page Dec 5, 2025
·
2 revisions
Complete beginner-friendly guide to getopt on Linux, covering Arch Linux, CachyOS, and other distributions including command-line option parsing, argument parsing, and script option handling.
Basic usage:
# Parse options
getopt -o "abc:" -- "$@"
# -o = options (a, b, c with argument)Short flags:
# Short options
getopt -o "hv" -- "$@"
# -h = help
# -v = verboseOptions with values:
# Option with argument
getopt -o "f:" -- "$@"
# -f = file (requires argument)Multiple flags:
# Multiple options
getopt -o "abc:d" -- "$@"
# a, b, c (with arg), dLong options:
# Long options
getopt -l "help,verbose,file:" -- "$@"
# --help, --verbose, --file=valueShort and long:
# Both formats
getopt -o "hv" -l "help,verbose" -- "$@"
# Supports both -h and --helpScript example:
#!/bin/bash
OPTS=$(getopt -o "hv" -l "help,verbose" -- "$@")
eval set -- "$OPTS"
while true; do
case "$1" in
-h|--help)
echo "Help message"
shift
;;
-v|--verbose)
VERBOSE=1
shift
;;
--)
shift
break
;;
esac
doneCheck installation:
# getopt is part of util-linux
# Usually pre-installed
# Check getopt
which getoptThis guide covered getopt usage, option parsing, and command-line argument handling for Arch Linux, CachyOS, and other distributions.
- Bash Scripting Guide - Scripting basics
- getopts Guide - Built-in option parsing
-
getopt Documentation:
man getopt
This guide covers Arch Linux, CachyOS, and other Linux distributions. For distribution-specific details, refer to your distribution's documentation.