Long options and end-of-options
Long options such as --name VALUE are a CLI design convention, not a feature of Bash getopts. Parse them with a reviewed while and case loop. Define whether --name=VALUE is also allowed, whether empty values are permitted, and how repeated options behave. A standalone -- ends option parsing; consume it and preserve the remaining operands as an array. A parser can stop at the first operand or permit mixed options; this lesson chooses the first-operand rule. Unknown options must produce a usage error, not be silently accepted. The external util-linux getopt can normalize long options, but it is a separate executable with implementation-specific quoting and compatibility. Its shell-quoted output is not an argument array. Avoid evaluating constructed strings; the examples use explicit parsing instead. The example treats a lone hyphen as an operand, stops at the first operand, and consumes the next token as a required value even when it begins with a hyphen. The util-linux getopt probe has a special expected status of 4.
Practical examples
Run each example as a separate script in a fresh noninteractive Bash 5.2 process and empty practice directory. Tests use C locale; do not enter expected output as code.
Parse explicit long options
parse() { local name=guest while (( $# )); do case $1 in --name) if (( $# < 2 )); then printf 'missing name\n' >&2; return 2 fi name=$2; shift 2 ;; --name=*) name=${1#*=}; shift ;; --) shift; break ;; -?*) printf 'unknown: %s\n' "$1" >&2; return 2 ;; *) break ;; esac done if [[ -z $name ]]; then printf 'empty name\n' >&2; return 2 fi local -a operands=("$@") printf 'name=[%s] count=%s\n' "$name" "${#operands[@]}" if (( ${#operands[@]} )); then printf '[%s]\n' "${operands[@]}"; fi}parse --name first '--name=Ada Lovelace' -- -draft '' ''Expected stdout:
name=[Ada Lovelace] count=3[-draft][][]The last name wins, -- ends options, and three operands remain (two empty). Values may start with a dash in this contract.
Forward a leading-hyphen operand
printf 'draft\n' > ./-draft || exit 1show() { if [[ ${1-} == -- ]]; then shift; fi if (( $# != 1 )); then return 2; fi cat -- "$1"}show -- -draftExpected stdout:
draftThe first -- belongs to show; the second belongs to cat. They serve separate parsers.
Inspect external getopt identity
printf 'builtin: 'type -t getoptsprintf 'external: 'type -t getoptgetopt --teststatus=$?printf 'enhanced test status=%s\n' "$status"Expected stdout:
builtin: builtinexternal: fileenhanced test status=4The installed enhanced util-linux getopt returns 4 for --test by design. This is a capability probe, not general success.
Common mistakes
Never use eval to reconstruct a command from arbitrary user arguments. A quoted filename starting with a hyphen may still be treated as an option: use the target utility’s supported -- or a suitable path prefix.
Practice
Extend the explicit parser to accept --format text and --format=text. Reject unknown formats and missing values; preserve an operand named -draft after --. Test repeated format options.
Next: Help, usage errors and dry-run modes