Skip to content

Latest commit

 

History

History
224 lines (178 loc) · 4.28 KB

File metadata and controls

224 lines (178 loc) · 4.28 KB
  • select is a Bash construct for generating interactive menus.

  • Syntax looks like a for loop:

    select ITEM in [LIST]
    do
      [COMMANDS]
    done
  • [LIST] can be:

    • A list of strings separated by spaces
    • A range of numbers
    • Command substitution output
    • An array
  • The menu is displayed with numbered options.

  • User chooses by entering a number:

    • ITEM → holds the chosen value
    • REPLY → holds the number entered
  • A custom prompt is set with PS3.

Example 1 – Simple Menu

PS3="Enter a number: "
select character in Sheldon Leonard Penny Howard Raj
do
    echo "Selected character: $character"
    echo "Selected number: $REPLY"
done
  • Displays:

    1) Sheldon
    2) Leonard
    3) Penny
    4) Howard
    5) Raj
    Enter a number:
    

Example 2 – Calculator (integer operations)

#!/bin/bash

PS3="Select the Operation: "

echo "Calculator"
select opr in addition substraction multiplication division quit; do 
    case $opr in 
        addition)
            read -p "Enter a first number:"  num1
            read -p "Enter a second number:" num2
            echo "$num1 + $num2 = $(( $num1 + $num2 ))"
            ;;
        substraction)
            read -p "Enter a first number: " num1
            read -p "Enter a second number: " num2
            echo "$num1 - $num2 = $(( $num1 - $num2 ))"
            ;;
        multiplication)
            read -p "Enter a first number: " num1
            read -p "Enter a second number: " num2
            echo "$num1 * $num2 = $(( $num1 * $num2 ))"
            ;;
        division)
            read -p "Enter a first number: " num1
            read -p "Enter a second number: " num2
            echo "$num1 / $num2 = $(( $num1 / $num2 ))"
            ;;
        quit)
            break
            ;;
        *)
        echo "Invalid option $REPLY"
            ;;
    esac
done

Example 3 – Advanced Calculator (supports floating numbers with bc)

calculate () {
  read -p "Enter the first number: " n1
  read -p "Enter the second number: " n2
  echo "$n1 $1 $n2 = " $(bc -l <<< "$n1$1$n2")
   echo "$n1 $1 $n2 = $(expr $n1 "$1" $n2)" 
}

PS3="Select the operation: "

select opt in add subtract multiply divide quit; do
  case $opt in
    add) calculate "+" ;;
    subtract) calculate "-" ;;
    multiply) calculate "*" ;;
    divide) calculate "/" ;;
    quit) break ;;
    *) echo "Invalid option $REPLY" ;;
  esac
done
#!/bin/bash

PS3="please select os? "   # Custom prompt for select

select os in linux windows mac
do
    case ${os} in
        linux)
            echo "you selected linux"
            echo "thanks."
            break
            ;;
        windows)
            echo "you selected windows"
            echo "thanks."
            break
            ;;
        mac)
            echo "you selected mac"
            echo "thanks."
            break
            ;;
        *)
            echo "Invalid"
            ;;
    esac
done

🔎 How it Works

  1. PS3 → sets the prompt shown when select asks for input.

  2. select os in ... → shows a numbered menu:

    1) linux
    2) windows
    3) mac
    please select os?
    
  3. User enters a number (e.g., 1) → the variable $os is set (linux).

  4. case statement handles each possible choice.

  5. * case handles invalid/empty input.

  6. break → exits the loop after a valid selection.


🔹 Example Runs

Case 1: Valid input

1) linux
2) windows
3) mac
please select os? 1
you selected linux
thanks.

Case 2: Invalid input

please select os? 5
Invalid

✅ Improvement (More Robust)

To avoid looping forever on invalid input, add an empty check:

#!/bin/bash

PS3="please select os? "

select os in linux windows mac
do
    if [[ -z "$os" ]]; then
        echo "Invalid option"
        continue
    fi

    case $os in
        linux|windows|mac)
            echo "you selected $os"
            echo "thanks."
            break
            ;;
    esac
done

Now:

  • If input is invalid (5, 10, blank), it prints Invalid option and asks again.
  • If input is valid, it executes and exits.

💡 Pro Tip: You can also use select for menus like:

  • File selection
  • Service management
  • Script option pickers