πŸ”§ 4. Bash Functions & Scripting
Estimated reading: 3 minutes 76 views

🧭 Bash: Select Menus – Build Interactive Scripts with select Loop


🧲 Introduction to Bash select – Create Interactive Menus Easily

The select command in Bash is a powerful but underused feature that lets you create interactive command-line menus with minimal code. It’s perfect for prompting users to pick from a list of optionsβ€”ideal for scripts involving file choices, configuration menus, automation tasks, or even setup wizards.

Unlike read, which requires manual validation, select handles user input and numbering automatically, making it one of the simplest ways to gather structured choices in Bash scripts.


🎯 In this article, you’ll learn:

  • How the Bash select loop works
  • Syntax and best practices for menu creation
  • Real-world use cases with examples
  • How to handle invalid input and loop menus

🧾 Basic Syntax of select

select variable in list; do
  commands
done
PartDescription
variableThe chosen item from the list
listMenu options presented to the user
do blockExecutes after user makes a selection

πŸ’‘ The user’s choice is stored in $REPLY and matched to the selected item.


πŸ§ͺ Example 1: Simple Select Menu

#!/bin/bash

select fruit in Apple Banana Orange Exit; do
  case $fruit in
    Apple) echo "You chose Apple";;
    Banana) echo "You chose Banana";;
    Orange) echo "You chose Orange";;
    Exit) break;;
    *) echo "Invalid option";;
  esac
done

βœ… Output:

1) Apple
2) Banana
3) Orange
4) Exit
#? 2
You chose Banana

πŸ§ͺ Example 2: Loop Until Exit Selected

#!/bin/bash

echo "Select a file action:"
PS3="Enter your choice: "

select action in Create Delete Rename Quit; do
  case $action in
    Create)
      echo "Creating a file..."
      ;;
    Delete)
      echo "Deleting a file..."
      ;;
    Rename)
      echo "Renaming a file..."
      ;;
    Quit)
      echo "Exiting."
      break
      ;;
    *)
      echo "Invalid selection."
      ;;
  esac
done

βœ… Output:

1) Create
2) Delete
3) Rename
4) Quit
Enter your choice: 

βš™οΈ Customizing the Prompt with PS3

By default, the prompt is #? . You can change it by setting PS3 before select:

PS3="Choose an option (1-4): "

🧠 PS3 controls the text shown when prompting for user input.


πŸ› οΈ Accessing Raw User Input with $REPLY

Even if a user enters a number that doesn’t match an option, you can access it using $REPLY.

select opt in Start Stop Restart; do
  echo "You selected '$opt' (input number: $REPLY)"
done

🧠 Best Practices for select Menus

PracticeWhy It Helps
Add an β€œExit” or β€œQuit” optionAllows graceful script termination
Handle *) caseCatches invalid or blank input
Use break to exit loopsPrevents infinite user prompting
Set a descriptive PS3 promptImproves user experience

πŸ“Œ Summary: Bash Select Menus

The select loop is a clean, interactive way to prompt users in Bash. It requires little code, provides built-in numbering, and enhances usability for scripts with choices.

πŸ” Key Takeaways:

  • Use select to create interactive numbered menus
  • Use $REPLY to access raw user input
  • Combine with case to build powerful CLI tools
  • Set PS3 to customize prompts

βš™οΈ Real-world Uses:

  • Scripted installers or setup menus
  • File management utilities
  • Configuration scripts for services
  • User-interactive backup or sync tools

❓ FAQ – Bash Select Menus


❓ What is the default prompt in select?
βœ… It is #? . You can change it using:

PS3="Choose your option: "

❓ How do I break out of a select loop?
βœ… Use the break command in a menu case like:

Exit) break ;;

❓ What if the user enters an invalid number?
βœ… Handle it with:

*) echo "Invalid option" ;;

❓ Can I use select without case?
βœ… Yes, but using case makes branching and validation easier.


❓ How do I get the number the user typed?
βœ… Use the special variable $REPLY.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

🟒 Bash: Select Menus (select)

Or Copy Link

CONTENTS
Scroll to Top