params

[!TIP|label:see also:]

shell parameter parsers

PARSER STYLE
DESCRIPTION
PROS
CONS
PORTABILITY

🔸 case + shift loop

Manual parsing with a while + case combo

- Highly customizable - Simple

- Error-prone for complex flags - No automatic help

✅ POSIX-compliant

🔸 getopts (built-in)

Built-in short-option parser

- Easy for short options (-x)

- No long options - No auto help

✅ POSIX-compliant

🔹 getopt (external)

External tool to parse long/short flags

- Supports long & short - Auto reordering

- Not always installed - Not portable across BSD/Linux

⚠️ Not portable

🔸 bash-argparse (lib)

Bash-based helper library

- Structured, powerful - Python-like syntax

- Adds dependency - Not always installed

❌ Bash-only

🔹 docopts (doc-style)

Parses from usage doc string

- Clean UX - Declarative

- Heavy - Requires Python or external

❌ External

🔸 argbash (codegen)

Generates parsing boilerplate

- Auto docs/help - Safe/robust

- Needs build step

❌ Not runtime native

🔹 shflags (Google)

Lightweight Bash lib

- Google-style - Nice syntax

- Needs sourcing a lib

⚠️ Bash-only

PARSER
SHORT OPTS
LONG OPTS
PORTABLE
HELP GEN
EXTERNAL?

Manual Case

✅ (manual)

✅ POSIX

getopts

✅ POSIX

getopt

✅ (GNU)

bash-argparse

❌ Bash

docopts

✅ (Python)

argbash

✅ (tool)

shflags

✅ (lib)

pass parameters to another script

[!NOTE]

  • objective:

  • b.sh

  • a.sh

  • result

Manual Case-Loop Parser

[!NOTE]

  • ✅ Pros: Simple, portable

  • ❌ Cons: No built-in validation or help

  • result

Bash Equals-Separated

with one or more values

[!TIP]

  • positional_args+=("$1") to add the arguments to an array one by one

additional params on --

shift with uncertain params

POSIX getopts Parser

[!NOTE]

  • ✅ Pros: POSIX, built-in

  • ❌ Cons: No long options, no multi-word values

GNU getopt Parser

[!NOTE]

  • MacOS: brew install gnu-getopt

  • ✅ Pros: Short + long, nice user UX

  • ❌ Cons: Not portable across BSD/macOS by default (GNU-only)

bash-argparse Library

[!NOTE]

  • ✅ Pros: Clean, descriptive, built-in help

  • ❌ Cons: Bash-only, external lib needed

docopts

[!NOTE]

  • ✅ Pros: Elegant, self-documenting

  • ❌ Cons: Requires Python + docopts installed

argbash Code Generator

[!NOTE]

shflags (Google-style shell option lib)

[!NOTE]

references

Last updated