Select Statement

Bash Select Statement Examples

Using the select statement


The "select" statement is used to create simple menus from within a script. The basic syntax of a select statement is as follows:

Select Statement Syntax




select varname in list
do
   commands1
   commands2
   commands3
done

Select Statement Simple Menu



#!/bin/bash
#
clear
PS3="Select model of car: "

select model in Astra Fiesta Jaguar Focus Lotus Corsa Mini Mondeo Insignia Sportage

do
  echo "You have chosen $model"
done

In the above example we utilised the PS3 prompt setting to modify our prompt. The output from this interactive script can be seen below:

Select Statement Simple Menu



1) Astra       3) Jaguar     5) Lotus	   7) Mini	 9) Insignia
2) Fiesta      4) Focus	     6) Corsa	   8) Mondeo	10) Sportage
Select model of car: 2
You have chosen Fiesta
Select model of car: 6
You have chosen Corsa

Combining Select with Case


More functionality can be added to our menu script if we combine our select statement with that of a case statement
(see Case Statements)



#!/bin/bash
#
clear
PS3="Select model of car: "

select model in Astra Fiesta Jaguar Focus Lotus Corsa Mini Mondeo Insignia Sportage Quit

do
  case $model in
          Astra)
               echo "********************"
               echo "Astra is a popular family car manufactured by Vauxhall"
               echo "********************"
               ;;
         Fiesta)
               echo "********************"
               echo "Fiesta is a popular small car manufactured by Ford"
               echo "********************"
               ;;
         Jaguar)
               echo "********************"
               echo "Jaguar is a popular Executive Car"
               echo "********************"
               ;;

          Focus)
               echo "********************"
               echo "Focus is a popular family car manufactured by Ford"
               echo "********************"
               ;;

          Lotus)
               echo "********************"
               echo "Lotus is a high performance Sports Car"
               echo "********************"
               ;;
          Corsa)
               echo "********************"
               echo "Corsa is a popular family car manufactured by Vauxhall"
               echo "********************"
               ;;
           Mini)
               echo "********************"
               echo "mini is a popular car manufactured by BMW"
               echo "********************"
               ;;
         Mondeo)
               echo "********************"
               echo "Mondeo is a popular family car manufactured by Ford"
               echo "********************"
               ;;
      Insignia)
               echo "********************"
               echo "Insignia is a popular family car manufactured by Vauxhall"
               echo "********************"
               ;;
       Sportage)
               echo "********************"
               echo "Sportage is a popular family car manufactured by KIA"
               echo "********************"
               ;;
           Quit)
               echo "********************"
               echo "Exiting Menu"
               echo "********************"
               exit
               ;;
              *)
               echo "********************"
               echo "Please choose a valid entry (1-11)"
               echo "********************"
   esac
done

Output from the above Script



1) Astra       4) Focus	     7) Mini	  10) Sportage
2) Fiesta      5) Lotus	     8) Mondeo	  11) Quit
3) Jaguar      6) Corsa	     9) Insignia
Select model of car: 1
********************
Astra is a popular family car manufactured by Vauxhall
********************
Select model of car: 3
********************
Jaguar is a popular Executive Car
********************
Select model of car: 11
********************
Exiting Menu
********************

In the above script we also added provision for a user to exit the program. Otherwise the user would have to issue the "Ctrl +C" signal to exit.