How to show a commandline/terminal menu

You might have seen a lot of command line tools offering a small menu with a number of options to choose from. Also you might have written some bash scripts that you want to make available in your terminal as a menu. Here you can find a good example to get started.

Sample



This menu sample code is organized into three main chunks. "show_menu" function takes care of styling and displaying options. option_picked function is used to show the option selected in a different color. Finally there is a while loop that is used to show the options continuously til you exit by hitting just enter.

Use cases

For an example, in Salesforce context you can use this script as a starter to get started with Salesforce DX commands. Salesforce DX commands are notoriously long and difficult to remember. You can setup a bash script with different options and user friendly labels. Then using the sample menu you can invoke necessary sfdx commands.
As final step you can alias the path to the bash script in you ".bash_profile" file. This will let you call your utility script anytime from terminal without worrying about path.

alias heydx="sh /Users/myname/Documents/Scripts/dxUtility.sh"

1 comment: