Home » BASH » Write a bash script to making a simple multi-level user menu using select

Write a bash script to making a simple multi-level user menu using select

Update on:
Oct 3, 2021

Select works using a provided list (for example, it can be a wildcard selection for files) and will give you a list, such as:

[et_pb_dmb_code_snippet code=”U2VsZWN0IGEgZmlsZSBmcm9tIHRoZSBsaXN0OiAKMS4pIG15Zmlyc3QuZmlsZSAKMi4pIG15c2Vjb25kLmZpbGUgCllvdSBjaG9zZTogbXlzZWNvbmQuZmlsZQ==” _builder_version=”4.9.4″ _module_preset=”3a2d4e4b-f2ae-4571-a284-ca584312491f” hover_enabled=”0″ sticky_enabled=”0″]U2VsZWN0IGEgZmlsZSBmcm9tIHRoZSBsaXN0OiAKMS4pIG15Zmlyc3QuZmlsZSAKMi4pIG15c2Vjb25kLmZpbGUgCllvdSBjaG9zZTogbXlzZWNvbmQuZmlsZQ==[/et_pb_dmb_code_snippet]

Clearly, a menu such as about is very trivial; it can be useful for utility functions and for repeatable subtasks like deleting users or modifying files/archives. 


Select is already a part of the Bash shell, but it has a few less than obvious points. Select relies on three variables:

  • PS3: The prompt that’s echoed to the user before the menu is created
  • REPLY: The index of the item selected from the array
  • opt: The value of the item selected from the array—not the index

Technically, opt is not mandatory, but it is the value of the element being iterated by Select in our example. You could use another name and call it element, for example.

How to do it…

Let’s start our activity as follows:

Open a terminal and create a script called select_menu.sh with the following contents:

[et_pb_dmb_code_snippet title=”select_menu.sh” code=”IyEvYmluL2Jhc2ggClRJVExFPSJTZWxlY3QgZmlsZSBtZW51IiAKUFJPTVBUPSJQaWNrIGEgdGFzazoiIApPUFRJT05TPSgibGlzdCIgImRlbGV0ZSIgIm1vZGlmeSIgImNyZWF0ZSIpIAoKZnVuY3Rpb24gbGlzdF9maWxlcygpIHsgCglQUzM9IkNob29zZSBhIGZpbGUgZnJvbSB0aGUgbGlzdCBvciB0eXBlIFwiYmFja1wiIHRvIGdvIGJhY2sgdG8gdGhlIG1haW46ICIgCglzZWxlY3QgT1BUIGluICo7IGRvIAoJCWlmIFtbICRSRVBMWSAtbGUgJHsjT1BUW0BdfSBdXTsgdGhlbiAKCQkJaWYgW1sgIiRSRVBMWSIgPT0gImJhY2siIF1dOyB0aGVuIAoJCQkJcmV0dXJuIAoJCQlmaSAKCQkJZWNobyAiJE9QVCB3YXMgc2VsZWN0ZWQiIAoJCWVsc2UgCgkJCWxpc3RfZmlsZXMgCgkJZmkgCglkb25lIAp9″ _builder_version=”4.9.4″ _module_preset=”369a0a83-f997-4079-bcdc-749886c9e1c7″ hover_enabled=”0″ sticky_enabled=”0″]IyEvYmluL2Jhc2ggClRJVExFPSJTZWxlY3QgZmlsZSBtZW51IiAKUFJPTVBUPSJQaWNrIGEgdGFzazoiIApPUFRJT05TPSgibGlzdCIgImRlbGV0ZSIgIm1vZGlmeSIgImNyZWF0ZSIpIAoKZnVuY3Rpb24gbGlzdF9maWxlcygpIHsgCglQUzM9IkNob29zZSBhIGZpbGUgZnJvbSB0aGUgbGlzdCBvciB0eXBlIFwiYmFja1wiIHRvIGdvIGJhY2sgdG8gdGhlIG1haW46ICIgCglzZWxlY3QgT1BUIGluICo7IGRvIAoJCWlmIFtbICRSRVBMWSAtbGUgJHsjT1BUW0BdfSBdXTsgdGhlbiAKCQkJaWYgW1sgIiRSRVBMWSIgPT0gImJhY2siIF1dOyB0aGVuIAoJCQkJcmV0dXJuIAoJCQlmaSAKCQkJZWNobyAiJE9QVCB3YXMgc2VsZWN0ZWQiIAoJCWVsc2UgCgkJCWxpc3RfZmlsZXMgCgkJZmkgCglkb25lIAp9[/et_pb_dmb_code_snippet]

Execute the script using the following command:

[et_pb_dmb_code_snippet code=”JCBiYXNoIHNlbGVjdF9tZW51LnNo” _builder_version=”4.9.4″ _module_preset=”3a2d4e4b-f2ae-4571-a284-ca584312491f” hover_enabled=”0″ sticky_enabled=”0″]JCBiYXNoIHNlbGVjdF9tZW51LnNo[/et_pb_dmb_code_snippet]

Press 1 to enter the file list functionality. Enter the number of any files in the menu. Once satisfied, type “back” and press Enter to return to the main menu.

How script works…

Let’s understand our script in detail:

Creating the select_menu.sh script was trivial, but besides the use of select, some of the concepts should look familiar: functions, return, case statements, and recursion. 

The script enters the menu by calling the main_menu function and then proceeds to use select to generate a menu from the ${OPTIONS} array. The hard-coded variable named PS3 will output the prompt before the menu, and $REPLY contains the index of the item selected.

Pressing 1 and pressing Enter will cause select to walk through the items and then execute the list_files function. This function creates a submenu by using select for the second time to list all of the files in the directory. Selecting any directory will return a $OPT was selected message, but if back is entered, then the script will return from this function and call main_menu from within itself (recursion). At this point, you may select any items in the main menu.

Related Posts

Creating a config file and using it in tandem with your scripts

In this article, we are going to create a config file and use it in our shell script.PrerequisitesBesides having a terminal open, you need basic knowledge of creating scripts and config files.Write scriptNow, we are going to create a script and config file. The...

Calculating and reducing the runtime of a script

In this article, we are going to learn how to calculate and reduce the script’s runtime. A simple time command will help in calculating the execution time.PrerequisitesBesides having a terminal open, make sure you have the necessary scripts present in your...

Using Bash to monitor battery life and optimize it

In this article, we will learn about the TLP Linux tool. TLP is a command-line tool; it is used for power management and will optimize the battery life.PrerequisitesBesides having a Terminal open, you need to ensure that you have TLP installed on your system.How to do...

Creating a simple NAT and DMZ firewall using bash script

In this article, we will create a simple NAT firewall with DMZ using iptables.PrerequisitesBesides having a Terminal open, you need to ensure that iptables is installed in your machine.Write scriptWe will write a script to set up a DMZ using iptables. Create...

Follow Us

Our Communities

More on BASH

The Ultimate Managed Hosting Platform
Load WordPress Sites in as fast as 37ms!



Submit a Comment

Your email address will not be published.

two − 1 =