params
[!TIP|label:see also:]
shell parameter parsers
πΈ 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
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
getopts Parser[!NOTE]
β Pros: POSIX, built-in
β Cons: No long options, no multi-word values
GNU getopt Parser
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
bash-argparse Library[!NOTE]
β Pros: Clean, descriptive, built-in help
β Cons: Bash-only, external lib needed
docopts
docopts[!NOTE]
β Pros: Elegant, self-documenting
β Cons: Requires Python + docopts installed
argbash Code Generator
argbash Code Generator[!NOTE]
β Pros: Auto-generates robust scripts
β Cons: Requires preprocessing step
shflags (Google-style shell option lib)
shflags (Google-style shell option lib)[!NOTE]
β Pros: Easy-to-read, Google-style
β Cons: Needs external lib and sourcing
references
Last updated
Was this helpful?