select - Linux


The select command in Linux is used within shell scripts to create a menu from a list of items. It allows users to interact with the script by choosing an option from a list. This command is invaluable for interactive scripts and simplifies decision-making processes for the end user.


select varName in list; do
  # Commands to execute
  • varName : The name of the variable that will hold the selected option.
  • list : A space-separated list of choices presented to the user.
  • Commands inside the dodone block execute based on the user’s choice. Typically, a break is used to exit the loop after a choice is made.


The select command does not have options or flags. Its behavior is solely dictated by the syntax and logic provided in the script’s loop.


Basic Example

select drink in tea coffee water juice milk; do
  echo "You have chosen $drink"

This sample script presents a menu of drink options and prints the user’s selection.

Complex Example with Conditional Loop

select item in apple banana cherry "none of the above"; do
  case $item in
      echo "You selected $item."
    "none of the above")
      echo "No fruit selected."
      echo "Please select a valid option."

This script enhances the basic example with a case statement to handle multiple conditions.

Common Issues

Infinite Loops: Failing to include a break statement within the loop can cause the script to execute indefinitely.

  • Solution: Ensure a break statement is used to exit the loop after processing.

Unrecognized Selections: By default, select in an infinite loop will redo its prompt if an unrecognized input is entered.

  • Prevention: Use a default case in a case statement to handle unexpected inputs.


select is typically not used standalone in complex scripts but combined with commands like case, echo, and others to form a complete interactive script.

Example Using select with case:

select option in list exit; do
  case $option in
    list) ls ;;
    exit) break ;;
    *) echo "Invalid option." ;;

This script lets users choose to list directory contents or exit the menu.

  • case: Used within select to handle various selections.
  • echo: Commonly used to display choices or results within a select loop.

Additional resources for learning about shell scripting and these commands can be found in the official Bash documentation or Linux man pages.