0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Create a Menu Driven Program

Use a Bash script with a while loop and case statement to display a menu and handle user choices, like: while true; do echo "1) Option1"; read choice; case $choice in 1) echo "You chose 1";; 2) exit;; *) echo "Invalid option";; esac; done.
📋

Examples

Input1
OutputYou chose Option 1
Input3
OutputInvalid option, please try again.
Input2
OutputExiting program.
🧠

How to Think About It

To build a menu driven program in Bash, repeatedly show the user a list of options using a loop, get their input, and use a case statement to run code based on the choice. Keep looping until the user chooses to exit.
📐

Algorithm

1
Display menu options to the user
2
Read user input
3
Use a case statement to match the input to options
4
Perform the action for the chosen option
5
Repeat until the user selects the exit option
💻

Code

bash
#!/bin/bash
while true; do
  echo "Menu:"
  echo "1) Say Hello"
  echo "2) Exit"
  read -p "Enter choice: " choice
  case $choice in
    1) echo "Hello!";;
    2) echo "Exiting program."; break;;
    *) echo "Invalid option, please try again.";;
  esac
done
Output
Menu: 1) Say Hello 2) Exit Enter choice: 1 Hello! Menu: 1) Say Hello 2) Exit Enter choice: 3 Invalid option, please try again. Menu: 1) Say Hello 2) Exit Enter choice: 2 Exiting program.
🔍

Dry Run

Let's trace choosing option 1, then invalid option 3, then exit with 2.

1

Show menu and read input

Menu displayed, user inputs '1'

2

Process choice 1

Prints 'Hello!' and loops back

3

Show menu and read input

Menu displayed, user inputs '3'

4

Process invalid choice

Prints 'Invalid option, please try again.' and loops back

5

Show menu and read input

Menu displayed, user inputs '2'

6

Process exit choice

Prints 'Exiting program.' and breaks loop

StepUser InputOutput
11Hello!
23Invalid option, please try again.
32Exiting program.
💡

Why This Works

Step 1: Loop for continuous menu

The while true loop keeps the menu running until the user chooses to exit.

Step 2: Reading user input

The read command waits for the user to enter their choice.

Step 3: Handling choices with case

The case statement matches the input to options and runs the corresponding commands.

Step 4: Exiting the loop

When the user selects the exit option, break stops the loop and ends the program.

🔄

Alternative Approaches

Using select command
bash
#!/bin/bash
PS3='Choose an option: '
select opt in "Say Hello" "Exit"; do
  case $opt in
    "Say Hello") echo "Hello!";;
    "Exit") echo "Exiting program."; break;;
    *) echo "Invalid option.";;
  esac
done
The <code>select</code> command automatically creates a numbered menu and reads input, simplifying menu creation but with less control over formatting.
Using functions for options
bash
#!/bin/bash
say_hello() { echo "Hello!"; }
while true; do
  echo "1) Say Hello"
  echo "2) Exit"
  read -p "Enter choice: " choice
  case $choice in
    1) say_hello;;
    2) echo "Exiting program."; break;;
    *) echo "Invalid option.";;
  esac
done
Using functions improves readability and organization, especially for larger menus.

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

Time Complexity

The loop runs until the user exits, so time depends on user input count, making it O(n) where n is number of interactions.

Space Complexity

Uses constant space for variables and no extra data structures, so O(1).

Which Approach is Fastest?

All approaches run quickly; using select is simpler but less flexible, while functions improve code clarity without affecting speed.

ApproachTimeSpaceBest For
while + case loopO(n)O(1)Simple, flexible menus
select commandO(n)O(1)Quick menu creation with less code
functions for optionsO(n)O(1)Organized code for complex menus
💡
Use read -p to prompt and get user input on the same line for cleaner menus.
⚠️
Forgetting to include a loop causes the menu to show only once and exit immediately.