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

Was this helpful?