I'm creating a simple bash script and I want to create a select menu in it, like this:
$./script
echo "Choose your option:"
1) Option 1
2) Option 2
3) Option 3
4) Quit
And according to user's choice, I want different actions to be executed. I'm a bash shell scripting noob, I've searched the web for some answers, but got nothing really concrete.
Add
break
statements wherever you need theselect
loop to exit. If abreak
is not performed, theselect
statement loops and the menu is re-displayed.In the third option, I included variables that are set by the
select
statement to demonstrate that you have access to those values. If you choose it, it will output:You can see that
$REPLY
contains the string you entered at the prompt. It is used as an index into the array${options[@]}
as if the array were 1 based. The variable$opt
contains the string from that index in the array.Note that the choices could be a simple list directly in the
select
statement like this:but you can't put such a list in a scalar variable because of the spaces in one of the choices.
You can also use file globbing if you are choosing among files:
Using
dialog
, the command would look like this:Putting it in a script:
Not a new answer per se, but since there's no accepted answer yet, here are a few coding tips and tricks, for both select and zenity:
Worth mentioning:
Both will loop until the user explicitly chooses Quit (or Cancel for zenity). This is a good approach for interactive script menus: after a choice is selected and action performed, menu is presented again for another choice. If choice is meant to be one-time only, just use
break
afteresac
(the zenity approach could be further reduced also)Both
case
are index-based, rather than value-based. I think this is easier to code and maintainArray is also used for
zenity
approach."Quit" option is not among the initial, original options. It is "added" when needed, so your array stay clean. Afterall, "Quit" is not needed for zenity anyway, user can just click "Cancel" (or close the window) to exit. Notice how both uses the same, untouched array of options.
PS3
andREPLY
vars can not be renamed.select
is hardcoded to use those. All other variables in script (opt, options, prompt, title) can have any names you want, provided you do the adjustmentsYou can use this simple script for creating options
I have one more option that is a mixture of these answers but what makes it nice is that you only need to press one key and then the script continues thanks to the
-n
option of read. In this example, we are prompting to shutdown, reboot, or simply exit the script usingANS
as our variable and the user only has to press E, R, or S. I also set the default to exit so if enter is pressed then the script will exit.Since this is targeted at Ubuntu you should use whatever backend debconf is configured to use. You can find out the debconf backend with:
If it says "dialog" then it likely uses
whiptail
ordialog
. On Lucid it'swhiptail
.If that fails, use bash "select" as explained by Dennis Williamson.
Bash fancy menu
Try it out first, then visit my page for detailed description ... No need for external libraries or programs like dialog or zenity ...
I have used Zenity, which seems always there in Ubuntu, works very well and has many capabilities. This is a sketch of a possible menu:
There is already the same question in serverfault answered. The solution there uses whiptail.