LinuxCommandLibrary

choose

Select items from a list interactively

TLDR

Print the 5th item from a line (starting from 0)

$ choose [4]
copy

Print the first, 3rd, and 5th item from a line, where items are separated by ':' instead of whitespace
$ choose [[-f|--field-separator]] '[:]' [0] [2] [4]
copy

Print everything from the 2nd to 5th item on the line, including the 5th
$ choose [1]:[4]
copy

Print everything from the 2nd to 5th item on the line, excluding the 5th
$ choose [[-x|--exclusive]] [1]:[4]
copy

Print the beginning of the line to the 3rd item
$ choose :[2]
copy

Print all items from the beginning of the line until the 3rd item (exclusive)
$ choose [[-x|--exclusive]] :[2]
copy

Print all items from the 3rd to the end of the line
$ choose [2]:
copy

Print the last item from a line
$ choose [-1]
copy

SYNOPSIS

choose [options] [item ...]

PARAMETERS

-i, --ignore-case
    Ignores case when filtering or matching items.

-p PROMPT, --prompt=PROMPT
    Specifies the custom prompt string to display to the user. Defaults to "Choose: ".

-r, --regex
    Interprets the user's input as a regular expression for filtering, rather than a simple substring match.

-s, --show-items
    Displays the raw values of the items alongside their numbered names (if applicable).

-v, --version
    Prints the version information of the choose utility and exits.

--help
    Displays a brief help message detailing usage and options, then exits.

DESCRIPTION

The choose command is a compact yet powerful command-line utility designed for interactive item selection from a given list.

It primarily serves as a convenient tool for shell scripting, enabling users to present a list of options and receive a single, chosen item back. Input can be provided either as command-line arguments or via standard input (e.g., from a pipe).

Upon execution, choose displays the available items, often numerically or with their raw values, along with a customizable prompt. Users can then type part of an item's name or an index to filter the list dynamically. Once an item is selected (typically by pressing Enter), the chosen item is printed to standard output, making it easily usable in subsequent commands or script variables.

Its simplicity makes it ideal for quick, interactive decisions within automated workflows or daily command-line tasks where a full-fledged fuzzy finder might be overkill. It effectively bridges the gap between static scripting and dynamic user interaction.

CAVEATS

The choose command is not a standard utility universally available on all Linux distributions by default; it often requires installation via a specific package (e.g., 'linux-choose').

While useful for simple selection, it lacks advanced features like fuzzy matching or complex UI elements found in more sophisticated interactive tools such as fzf or percol.

INPUT METHODS

The choose command can receive its list of items in two primary ways:
1. Direct arguments: `choose item1 item2 "item with space"`
2. Standard input (pipe): `ls -1 | choose` or `cat items.txt | choose`

BASIC USAGE EXAMPLE

To select a file from the current directory interactively:
`selected_file=$(ls -1 | choose -p "Pick a file: ")`
`echo "You chose: $selected_file"`

HISTORY

The choose command emerged as a lean, focused utility to address the common need for simple interactive selection in shell scripts and command-line environments. Unlike core GNU utilities, its development is typically maintained by individual projects or smaller utility collections, aiming to provide a lightweight alternative to more feature-rich but potentially heavier interactive tools. Its prevalence varies across distributions, reflecting its role as a specialized helper rather than a foundational system command.

SEE ALSO

fzf(1), percol(1), dialog(1), whiptail(1), select (bash builtin)

Copied to clipboard