Name

select

Synopsis

select x [in list]
do
commands
done

Display a list of menu items on standard error, numbered in the order they are specified in list. If no in list is given, items are taken from the command line (via “$@”). Following the menu is a prompt string (set by the variable PS3). At the prompt, the user selects a menu item by typing its line number, or redisplays the menu by pressing the Enter key. User input is stored in the shell variable REPLY and the value selected is stored in x. If a valid item number is typed, the commands associated with the value in x are executed and the prompt is redisplayed for the user to select a new value. Typing EOF terminates the loop.

Example

PS3="Select the item number: "
select event in Format Page View Exit
do
   case "$event" in
     Format) nroff $file | lp;;
     Page)   pr $file | lp;;
     View)   more $file;;
     Exit)   exit 0;;
     *   )   echo "Invalid selection";;
   esac
done

The output of this script looks like this:

1. Format
2. Page
3. View
4. Exit
Select the item number:

Get Linux in a Nutshell, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.