Ubuntu: How can I create a select menu in a shell script?
Channel: Roel Van de Paar
Date: 07/06/2019

Ubuntu: How can I create a select menu in a shell script?


Question: 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.

Solutions Sample (Please watch the whole video to see all solutions, in order of how many people found them helpful):

== This solution helped 124 people ==
#!/bin/bash
# Bash Menu Script Example

PS3='Please enter your choice: '
options=("Option 1" "Option 2" "Option 3" "Quit")
select opt in "${options[@]}"
do
case $opt in
"Option 1")
echo "you chose choice 1"
;;
"Option 2")
echo "you chose choice 2"
;;
"Option 3")
echo "you chose choice $REPLY which is $opt"
;;
"Quit")
break
;;
*) echo "invalid option $REPLY";;
esac
done
Add break statements wherever you need the select loop to exit. If a break is
not performed, the select 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 chose choice 3 which is Option 3
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:
select opt in foo bar baz 'multi word choice'
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:
select file in *.tar.gz

== This solution helped 13 people ==
You can use this simple script for creating options
#!/bin/bash
echo "select the operation ************  1)operation 1 2)operation 2
3)operation 3 4)operation 4 "

read n
case $n in
1) commands for opn 1;;
2) commands for opn 2;;
3) commands for opn 3;;
4) commands for  opn 4;;
*) invalid option;;
esac

== This solution helped 2 people ==
There is already the same question in https://serverfault.com/questions/144939/
multi-select-menu-in-bash-script answered. The solution there uses whiptail.

With thanks & praise to God! With thanks to the many people who have made this project possible! | Content (except music & images) licensed under cc by-sa 3.0 | Music: https://www.bensound.com/royalty-free-music | Images: https://stocksnap.io/license & others | With thanks to user user360154 (https://askubuntu.com/users/360154), user txwikinger (https://askubuntu.com/users/4), user Rinzwind (https://askubuntu.com/users/15811), user MestreLion (https://askubuntu.com/users/11015), user Li Lo (https://askubuntu.com/users/289), user LazyEchidna (https://askubuntu.com/users/16522), user Jorge Castro (https://askubuntu.com/users/235), user jibin (https://askubuntu.com/users/23138), user HarlemSquirrel (https://askubuntu.com/users/109540), user dessert (https://askubuntu.com/users/507051), user Dennis Williamson (https://askubuntu.com/users/877), user Daniel Rodrigues (https://askubuntu.com/users/356), user Alex Lucard (https://askubuntu.com/users/133223), user Alaa Ali (https://askubuntu.com/users/52726), and the Stack Exchange Network (http://stackoverflow.com/questions/1705). Trademarks are property of their respective owners. Disclaimer: All information is provided "AS IS" without warranty of any kind. You are responsible for your own actions. Please contact me if anything should be amiss at Roel D.OT VandePaar A.T gmail.com.