LinuxCommandLibrary

select

Monitor multiple file descriptors for activity

TLDR

Create a menu out of individual words

$ select [word] in [apple orange pear banana]; do echo $[word]; done
copy

Create a menu from the output of another command
$ select [line] in $([command]); do echo $[line]; done
copy

Specify the prompt string for select and create a menu for picking a file or folder from the current directory
$ PS3="[Select a file: ]"; select [file] in *; do echo $[file]; done
copy

Create a menu from a Bash array
$ [fruits]=([apple orange pear banana]); select [word] in $[{fruits[@]]}; do echo $[word]; done
copy

SYNOPSIS

select name in word ... ; do commands ; done

Alternatively, if 'in word ...' is omitted, it defaults to positional parameters:
select name ; do commands ; done

PARAMETERS

name
    A variable name that will store the selected item's value (the string) within each iteration of the loop.

in word ...
    A list of words or strings that will be displayed as numbered menu options. If omitted, the shell's positional parameters ($@) are used as the list of choices.

do commands ; done
    The block of shell commands that will be executed each time the user makes a selection. This block typically processes the name variable (the selected item) and the REPLY variable (the user's entered number).

DESCRIPTION

The select construct is a powerful keyword in shells like bash and ksh, designed for creating interactive numbered menus within shell scripts. Unlike typical Linux commands, select is not an executable program but a fundamental part of the shell's scripting capabilities. It presents a list of choices to the user, each automatically numbered. The user enters the number corresponding to their desired option, and select then executes a block of commands. This mechanism simplifies user interaction by providing a structured way to gather input, making scripts more user-friendly and robust than relying solely on arbitrary read commands. The loop continues to prompt for input until explicitly exited or interrupted, ensuring a valid selection is typically made.

CAVEATS

The select construct is a shell built-in/keyword, not a standalone executable.
The loop continues indefinitely until a break, return, or exit command is encountered within the do...done block, or the script is interrupted (e.g., Ctrl+C).
The user's numeric input is stored in the REPLY shell variable, while the selected string from the list is assigned to the loop's name variable.
Error handling for invalid numeric input (e.g., out-of-range numbers, non-numeric input) must be explicitly managed within the do...done block.

REPLY VARIABLE

After a user makes a selection, the numeric input entered by the user (e.g., '1', '2', '3') is stored in the special shell variable REPLY. This is distinct from the name variable of the select loop, which holds the string value of the selected item.

LOOP TERMINATION

To exit a select loop, you must explicitly use a control statement like break within the do...done block, typically after processing a valid selection or if the user chooses an 'Exit' option. Without a break, the menu will continuously re-prompt for input after each action.

DEFAULT LIST (POSITIONAL PARAMETERS)

If the in word ... part is omitted from the select statement, the shell automatically uses the script's positional parameters ($1, $2, etc., represented as $@) as the list of choices for the menu. This is useful for creating menus based on command-line arguments.

HISTORY

The select construct originated in the KornShell (ksh) and was later adopted by the GNU Bash shell, becoming a standard feature for interactive shell scripting. Its inclusion provided a more structured and robust method for creating user menus compared to previous ad-hoc combinations of echo and read commands, significantly enhancing the interactivity and user-friendliness of shell scripts across various Unix-like systems.

SEE ALSO

read(1), case(1), while(1), bash(1), ksh(1)

Copied to clipboard