select

Expand words, generate a list of items.

Syntax
      select name [ in word ] ; do list ; done

The list of words following in is expanded, generating a list of items. The set of expanded words is printed on the standard error, each preceded by a number.

If the in word is omitted, the positional parameters are printed (see PARAMETERS). The PS3 prompt is then displayed and a line read from the standard input. If the line consists of a number corresponding to one of the displayed words, then the value of name is set to that word.

If the line is empty, the words and prompt are displayed again. If EOF is read, the command completes. Any other value read causes name to be set to null. The line read is saved in the variable REPLY.

The list is executed after each selection until a break command is executed.

The exit status of select is the exit status of the last command executed in list, or zero if no commands were executed.

select is a bash construct.

Example script

#!/bin/bash

echo "Enter the number of the file you want to view: (ctrl-C to quit)"

PS3="Your choice: "

select FILENAME in *;
do
  echo "You picked $FILENAME ($REPLY)"
done

“In expanding the field of knowledge, we but increase the horizon of ignorance” ~ Henry Miller

Related:

break - Exit from a loop
while - Execute commands
continue - Resume the next iteration of a while or foreach loop


© Copyright SS64.com 1999-2013
Some rights reserved