Select Menu, Linux Bash Script.
2 min readApr 1, 2023
Syntax
Select ITEM in LIST
do
COMMANDS
done
ITEM is a user defined variable and the LIST is a series of string, numbers of outputs of commands.
Simple menu script choose your distribution:
ubuntu:~/Bash_Script# vim menu.sh
1 #!/bin/bash
2 select distro in "Ubuntu" "Debian" "CentOS" "RHEL" "Archlinux"
3 do
4 echo "Distro is $distro"
5 echo "Reply is $REPLY"
6 done
ubuntu:~/Bash_Script# bash menu.sh
1) Ubuntu
2) Debian
3) CentOS
4) RHEL
5) Archlinux
#? 1
Distro is Ubuntu
Reply is 1
#? 2
Distro is Debian
Reply is 2
#? 3
Distro is CentOS
Reply is 3
#? 4
Distro is RHEL
Reply is 4
#? 5
Distro is Archlinux
Reply is 5
#?
Select Menu script and gives exit options as well:
ubuntu:~# vim menu_v2.sh
1 PS3="Choose your distro: ";
2 select distro in Ubuntu Debian Fedora Archlinux Quit
3 do
4 case $REPLY in
5 1)
6 echo "Your distro is Ubuntu" ;;
7 2)
8 echo "Your distro is Debian" ;;
9 3)
10 echo "Your distro is CentOS" ;;
11 4)
12 echo "Your distro is Archlinux" ;;
13 5)
14 echo "Exiting…"
15 break;;
16 *) echo "Replay is $REPLY";;
17 esac
18
19 done
ubuntu:~/Bash_Script# bash menu_v2.sh
1) Ubuntu
2) Debian
3) Fedora
4) Archlinux
5) Quit
Choose your distro: 1
Your distro is Ubuntu
Choose your distro: 2
Your distro is Debian
Choose your distro: 3
Your distro is CentOS
Choose your distro: 4
Your distro is Archlinux
Choose your distro: 5
Exiting…
Conclusion
The select component makes it simple to create menus. When writing shell scripts that demand user input, it is particularly helpful.