Skip to content

13. Bash Select

Overview

The Bash select statement is used to create a simple interactive selection menu in the terminal. It automatically displays numbered options and waits for the user to choose one.

It is useful when a Bash script needs the user to select one option from a predefined list.

Basic Syntax

select variable in option1 option2 option3
do
    commands
done

When the select statement runs, Bash:

  • Displays each option with a number.

  • Displays the value of PS3 as the prompt.

  • Waits for the user to enter a number.

  • Stores the selected option in the specified variable.

  • Executes the commands inside the do ... done block.

  • Continues displaying the menu until the loop is terminated.

Example

#!/bin/bash

PS3='Choose one word: '

select word in "linux" "bash" "scripting" "tutorial"
do
    echo "The word you have selected is: $word"

    # Break the loop after one selection
    break
done

exit 0

Output

$ ./select.sh
1) linux
2) bash
3) scripting
4) tutorial
Choose one word: 2
The word you have selected is: bash

Understanding the Script

Shebang

#!/bin/bash

Specifies that the script should be executed using Bash.

PS3

PS3='Choose one word: '

PS3 defines the prompt displayed by the select statement.

Without setting PS3, Bash uses the default prompt:

#

Using a custom prompt makes the menu easier for the user to understand.

select

select word in "linux" "bash" "scripting" "tutorial"

This creates a menu containing four options.

Bash automatically assigns numbers to the options:

1) linux
2) bash
3) scripting
4) tutorial

The selected value is stored in the variable word.

For example, if the user enters:

2

then:

word="bash"

do ... done

do
    echo "The word you have selected is: $word"
    break
done

The commands between do and done are executed after the user makes a selection.

break

break

A select statement normally continues running and displays the menu again after each iteration.

break terminates the loop after the first selection.

Without break, the menu would continue to appear:

1) linux
2) bash
3) scripting
4) tutorial
Choose one word:

Using select With case

select is commonly combined with case when different actions need to be performed for different selections.

#!/bin/bash

PS3="Select an option: "

select option in "Show Date" "Show Current Directory" "Exit"
do
    case $option in
        "Show Date")
            date
            ;;
        "Show Current Directory")
            pwd
            ;;
        "Exit")
            echo "Exiting..."
            break
            ;;
        *)
            echo "Invalid selection"
            ;;
    esac
done

Example Output

1) Show Date
2) Show Current Directory
3) Exit
Select an option: 1

Thu Sep 17 20:39:00 IST 2026

Selecting option 2 executes:

pwd

Selecting option 3 executes:

break

and terminates the menu.

Handling Invalid Input

If the user enters a number that does not correspond to an option, the selection variable is empty.

For example:

1) linux
2) bash
3) scripting
4) tutorial
Choose one word: 8
The word you have selected is:

A case statement can be used to handle this:

#!/bin/bash

PS3="Choose an option: "

select option in "Linux" "Bash" "Exit"
do
    case $option in
        "Linux")
            echo "You selected Linux"
            ;;
        "Bash")
            echo "You selected Bash"
            ;;
        "Exit")
            break
            ;;
        *)
            echo "Invalid selection"
            ;;
    esac
done

Important Variables

The select statement works with several useful Bash variables.

REPLY

REPLY contains the number entered by the user.

#!/bin/bash

PS3="Choose an option: "

select option in "Linux" "Bash" "Exit"
do
    echo "Selected number: $REPLY"
    echo "Selected option: $option"
    break
done

If the user selects option 2:

Choose an option: 2
Selected number: 2
Selected option: Bash

This is useful when you need both the selected option and its menu number.

Practical Example

A simple system administration menu can be created using select:

#!/bin/bash

PS3="Select an action: "

select action in "Show System Information" "Show Disk Usage" "Show Memory Usage" "Exit"
do
    case $REPLY in
        1)
            uname -a
            ;;
        2)
            df -h
            ;;
        3)
            free -h
            ;;
        4)
            echo "Exiting..."
            break
            ;;
        *)
            echo "Invalid selection"
            ;;
    esac
done

This creates an interactive menu where the user can choose different system administration commands.

Key Points

  • select creates interactive numbered menus in Bash.

  • PS3 controls the selection prompt.

  • The selected option is stored in the specified variable.

  • $REPLY contains the number entered by the user.

  • case can be combined with select to perform different actions.

  • break can terminate the selection loop.

  • Without break or another loop-control mechanism, the menu continues to appear.