Bash Select (Make Menus) and Bash select Example

In this tutorial, we will cover the basics of the select construct in Bash.

The select construct allows you to generate menus.

Bash select Construct

The select construct generates a menu from a list of items. It has almost the same syntax as the [for](https://linuxize.com/post/bash-for-loop/) loop:

select ITEM in [LIST]
do
  [COMMANDS]
done

Copy

The [LIST] can be a series of strings separated by spaces, a range of numbers, output of a command, an array, and so on. A custom prompt for the select construct can be set using the PS3 environment variable.

When the select construct is invoked, each item from the list is printed on the screen (standard error), preceded with a number.

If the user enters a number that corresponds to the number of one of the displayed items, then the value of [ITEM] is set to that item. The value of the selected item is stored in the variable REPLY. Otherwise, if the user input is empty, the prompt and the menu list are displayed again.

Theselectloop will continue to run and prompt for user input until the[break](https://linuxize.com/post/bash-break-continue/)command is executed.To demonstrate how the select construct works, let’s take a look at the following simple example:

PS3="Enter a number: "

select character in Sheldon Leonard Penny Howard Raj
do
    echo "Selected character: $character"
    echo "Selected number: $REPLY"
done

#bash #bash select construct #bash select

Bash Select (Make Menus) and Bash select Example
3.45 GEEK