0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Create Interactive Menu with User Input

Use the Bash select statement to create an interactive menu and handle user choices with a case block, for example: select option in Option1 Option2 Exit; do case $option in Option1) echo 'You chose Option1';; Option2) echo 'You chose Option2';; Exit) echo 'Exiting menu'; break;; *) echo 'Invalid choice';; esac; done.
📋

Examples

InputUser selects 1
OutputYou chose Option1
InputUser selects 3
OutputExiting menu
InputUser selects 5
OutputInvalid choice
🧠

How to Think About It

To create an interactive menu in Bash, use the select command which automatically displays options and waits for user input. Then use a case statement to run different commands based on the user's choice. Loop the menu until the user chooses to exit.
📐

Algorithm

1
Display menu options using <code>select</code>.
2
Wait for user input to choose an option.
3
Use <code>case</code> to match the choice and execute commands.
4
If the user selects exit, break the loop.
5
If the choice is invalid, show an error message.
6
Repeat the menu until exit is chosen.
💻

Code

bash
#!/bin/bash
PS3='Please enter your choice: '
options=("Option1" "Option2" "Exit")
select opt in "${options[@]}"
do
  case $opt in
    "Option1")
      echo "You chose Option1"
      ;;
    "Option2")
      echo "You chose Option2"
      ;;
    "Exit")
      echo "Exiting menu"
      break
      ;;
    *)
      echo "Invalid choice"
      ;;
  esac
done
Output
1) Option1 2) Option2 3) Exit Please enter your choice: 1 You chose Option1 1) Option1 2) Option2 3) Exit Please enter your choice: 3 Exiting menu
🔍

Dry Run

Let's trace selecting option 1 then option 3 through the code

1

Display menu

Shows options: 1) Option1 2) Option2 3) Exit

2

User inputs 1

Variable $opt is set to 'Option1'

3

Case matches Option1

Prints 'You chose Option1'

4

Display menu again

Shows options again

5

User inputs 3

Variable $opt is set to 'Exit'

6

Case matches Exit

Prints 'Exiting menu' and breaks loop

User InputValue of $optAction
1Option1Print 'You chose Option1'
3ExitPrint 'Exiting menu' and exit
💡

Why This Works

Step 1: Using select for menu

The select command automatically prints numbered options and waits for user input, simplifying menu creation.

Step 2: Handling choices with case

The case statement matches the selected option and runs the corresponding commands.

Step 3: Looping until exit

The menu repeats until the user selects the exit option, which breaks the loop and ends the script.

🔄

Alternative Approaches

Using while loop with read
bash
#!/bin/bash
while true; do
  echo "1) Option1"
  echo "2) Option2"
  echo "3) Exit"
  read -p "Enter choice: " choice
  case $choice in
    1) echo "You chose Option1";;
    2) echo "You chose Option2";;
    3) echo "Exiting menu"; break;;
    *) echo "Invalid choice";;
  esac
done
More manual control over menu display but requires extra code to print options.
Using select with functions
bash
#!/bin/bash
function option1() { echo "You chose Option1"; }
function option2() { echo "You chose Option2"; }
PS3='Choose: '
select opt in Option1 Option2 Exit; do
  case $opt in
    Option1) option1;;
    Option2) option2;;
    Exit) echo "Bye"; break;;
    *) echo "Invalid";;
  esac
done
Organizes code better by separating actions into functions.

Complexity: O(n) time, O(1) space

Time Complexity

The menu loops until exit, so time depends on user input count, making it O(n) where n is number of selections.

Space Complexity

Uses constant space for variables and options, so O(1) space complexity.

Which Approach is Fastest?

Using select is faster to write and read than manual loops, but both have similar runtime since user input dominates.

ApproachTimeSpaceBest For
select statementO(n)O(1)Quick menu with minimal code
while loop with readO(n)O(1)Custom menu display and input validation
select with functionsO(n)O(1)Organized code for complex actions
💡
Set the prompt with PS3 to customize the menu input message.
⚠️
Forgetting to handle invalid inputs causes the menu to accept empty or wrong choices silently.