Display user selction from bash menu

I am trying to display the text the user selects from a bash menu. The below will display the menu and allow the user to enter a number, but will not display the choice selected.

bash

while true
    do
        printf "\n please make a selection from the MENU \n
        ==================================
        \t 1  Incidental Findings
        \t 2  CHARGE Syndrome
        \t 3  PFS       
        ==================================\n\n"
        printf "\t Your choice: "; read menu_choice
        echo "$1"
done

So if the user selects 2 from the menu , then CHARGE Syndrome is displayed. Thank you :).

Assuming bash shell take a look at:-
help case
And search case exmples on here, there is a plethora of answers.

1 Like

That's what the select statement in bash is for:

PS3="please make a selection from the MENU: "
select CH in "Incidental Findings" "CHARGE Syndrome" "PFS"
  do echo "Your choice: $CH"
  done
2 Likes

It is possible to use an array index, too.

#!/bin/bash

CHOICES=("Incidental Findings" "CHARGE Syndrome" "PFS")

for ITEM in "${CHOICES[@]}";do
  ((i++))
  echo "$i $ITEM"
done
read -p "Your choice: " RESPONSE 
echo ${CHOICES[ $(( $RESPONSE - 1 )) ]}

"dialog" can do nice dialogs too...

dialog --menu Title 10 60 6 1 "Incidental Findings" 2 "CHARGE Syndrome" 3 "PFS"

---------- Post updated at 12:37 AM ---------- Previous update was at 12:04 AM ----------

Nice one. Never used it before. Thanks.

1 Like

Thank you all :slight_smile: