select

shell builtinLinux/Unix
The select command is one of the most frequently used commands in Linux/Unix-like operating systems. select Create an interactive selection menu in the shell

Quick Reference

Command Name:

select

Category:

shell builtin

Platform:

Linux/Unix

Basic Usage:

select [options] [arguments]

Common Use Cases

    Syntax

    select name [in words ...]; do commands; done

    Options

    Variable Description
    PS3 The prompt string displayed for the select command (default is "#? ")
    REPLY Contains the number entered by the user
    name The variable that receives the selected item
    IFS Internal Field Separator (affects how the items are split)

    Examples

    How to Use These Examples

    The examples below show common ways to use the select command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    select option in "Option 1" "Option 2" "Option 3" "Quit" do case $option in "Option 1") echo "You selected Option 1" ;; "Option 2") echo "You selected Option 2" ;; "Option 3") echo "You selected Option 3" ;; "Quit") echo "Exiting..." break ;; *) echo "Invalid option" ;; esac done
    Create a simple menu with four options.
    # Advanced Examples Advanced
    select file in $(ls *.txt) do if [ -n "$file" ]; then echo "You selected $file" cat "$file" break else echo "Invalid selection" fi done
    Create a menu to select and display a text file from the current directory.
    PS3="Choose a fruit: " select fruit in Apple Banana Orange "Exit menu" do if [ "$fruit" == "Exit menu" ]; then echo "Goodbye!" break elif [ -n "$fruit" ]; then echo "You chose $fruit" else echo "Invalid selection" fi done Customize the prompt message using the PS3 variable. select os in Linux Windows macOS do echo "REPLY variable contains: $REPLY" echo "Selected option: $os" break done Show the usage of the REPLY variable which contains the number entered by the user. # Combining with functions show_options() { select option in "View file" "Edit file" "Delete file" "Exit" do case $option in "View file") cat myfile.txt show_options ;; "Edit file") nano myfile.txt show_options ;; "Delete file") rm myfile.txt echo "File deleted" break ;; "Exit") echo "Exiting..." break ;; *) echo "Invalid option" show_options ;; esac done } show_options Create a recursive menu system using functions.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The `select` command is a powerful shell built-in that creates interactive menus in Bash and other shell scripts. It provides a simple way to present users with a numbered list of options and process their selection, making it ideal for creating user-friendly command-line interfaces within shell scripts. When the `select` command is executed, it generates a numbered menu from the provided list of items, displays a prompt (defined by the PS3 variable), and waits for the user to enter a selection. After the user inputs a number and presses Enter, the corresponding item is assigned to the specified variable, and the commands in the loop body are executed. Key features and behavior of the `select` command include: 1. Menu Generation: Automatically creates a numbered list of options from the provided items. 2. Loop Structure: Like other shell loops (while, for, until), select uses a do/done block to contain the commands to be executed for each selection. 3. User Input: Captures numeric input from the user and maps it to the corresponding menu option. 4. Special Variables: Works with PS3 (prompt string) and REPLY (contains the number entered by the user). 5. Iteration: Continues presenting the menu until explicitly terminated with a break statement or until the script ends. The `select` command is particularly useful for: - Creating interactive system administration scripts - Building configuration utilities that require user choices - Developing simple text-based user interfaces - Providing option menus in installation or setup scripts - Creating interactive troubleshooting or diagnostic tools When using `select`, it's common to combine it with a `case` statement to process different menu selections appropriately. This combination allows for clear, structured handling of various options without complex nested if statements. Although `select` is a simple construct, it can be used to build sophisticated interactive interfaces when combined with other Bash features like functions, variables, and conditional statements. It's important to note that `select` is specific to Bash and some other advanced shells; it's not available in all POSIX-compliant shells like dash or the original Bourne shell. For scripts that need to run in various environments, it may be worth checking for the availability of the `select` command or implementing alternative menu systems using more basic constructs like `while` loops with `read` commands.

    Related Commands

    These commands are frequently used alongside select or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the select command works in different scenarios.

    $ select
    View All Commands