fn: ask_selection
Purpose
Display a reusable numbered selection list and return the selected value or values.
Behavior
- Accepts the supplied items directly, making normal Bash array expansion usable.
- Uses single-select mode by default.
- Supports multi-select mode with comma-separated numbers and numeric ranges.
- Returns actual item values rather than list indexes.
- Accepts Q to cancel and return to the caller.
- Re-prompts after invalid selections.
Options
--label TEXT— Heading displayed above the selection list. Defaults to "Select an option".--var NAME— Variable that receives the selected value. In --multi mode NAME receives an indexed array of selected values. Defaults to selection.--multi— Enable selection of multiple values using entries such as 1,3,5-7.--items ITEM...— Values to display. This option must be the final option in the call.
Outputs (globals)
Variable named by --var. Scalar in single-select mode; indexed array in multi-select mode.
Returns
- 0 when a valid selection was stored.
- 1 when the user selects Q to cancel.
- 2 when no items are supplied or --var is not a valid shell identifier.
Usage
# Single selection returns one value in the target variable.
options=("Default" "Dark" "Testadura")
ask_selection --label "Theme" --var theme --items "${options[@]}"
# Multiple selection returns an indexed array of selected values.
groups=("Domain Admins" "File Server Users" "Accounting")
ask_selection --label "Groups" --var selected_groups --multi --items "${groups[@]}"
