orange Verified current stable Not installed? System Operations

Orange / Create Menu From Bash Array

Create Menu From Bash Array

Generate an interactive selection menu from an array of predefined values in Bash.

$
Terminal
<fruits>=(<apple orange pear banana>); select <word> in ${<fruits[@]>}; do echo $<word>; done

When To Use

When needing to prompt users for a selection from a defined array of options with little variability expected.

Pro Tip

Ensure the items in the array do not contain spaces. This can cause parsing issues during selection and unexpected behaviors in Bash arrays.

Command Builder

Tune the command before you copy it

Back to syntax
$
Generated Command
<fruits>=(<apple orange pear banana>); select <word> in ${<fruits[@]>}; do echo $<word>; done

Anatomy of Output

Understanding the result

1) apple Fruit Option

First option in the fruits selection menu.

2) orange Fruit Option

Second visible option for selection.

3) pear Fruit Option

Third selectable menu item.

4) banana Fruit Option

Last item available for user selection.

Troubleshooting

Common pitfalls

select: word: variable not set

Solution: Ensure the 'word' variable is defined correctly before the select command.

bash: bad substitution

Solution: Check for unquoted array definitions that may lead to incorrect parsing.

select: invalid option

Solution: Ensure the index number used is valid and corresponds to array elements.

Command Breakdown

What each part is doing

<fruits>=(<apple
Base Command
The executable that performs this operation. Here it runs Orange before the shell applies any redirect operators.
<fruits>
fruits
The value supplied for fruits.
<apple orange pear banana>
apple orange pear banana
The value supplied for apple orange pear banana.
<word>
word
The value supplied for word.
<fruits[@]>
fruits[@
The value supplied for fruits[@.

Alternative Approaches

Comparable commands in other tools

Alternative system operations tools for the same job.