Bash Script to Create Interactive Menu with User Input
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
How to Think About It
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
Code
#!/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
Dry Run
Let's trace selecting option 1 then option 3 through the code
Display menu
Shows options: 1) Option1 2) Option2 3) Exit
User inputs 1
Variable $opt is set to 'Option1'
Case matches Option1
Prints 'You chose Option1'
Display menu again
Shows options again
User inputs 3
Variable $opt is set to 'Exit'
Case matches Exit
Prints 'Exiting menu' and breaks loop
| User Input | Value of $opt | Action |
|---|---|---|
| 1 | Option1 | Print 'You chose Option1' |
| 3 | Exit | Print '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
#!/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
#!/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
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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| select statement | O(n) | O(1) | Quick menu with minimal code |
| while loop with read | O(n) | O(1) | Custom menu display and input validation |
| select with functions | O(n) | O(1) | Organized code for complex actions |
PS3 to customize the menu input message.