Search code examples
bashselectterminalmenumacos-sonoma

Why is the select menu not working in Mac terminal?


I was trying to create a menu in using select but alas the menu won't accept my answers and won't also let me quit the menu. I tried this:

code:

options=( "olá" "adeus" )
echo "escolha uma opção"
select opt in "${options[@]}"
do
    case opt in 
        "olá")
            echo "Olá de volta!"
            break
        ;;
        "adeus")
            echo "Mal criado chunga!"
            break
        ;;
    esac
done

This was the test in the terminal:

Last login: Wed Oct 11 10:27:23 on ttys000

The default interactive shell is now zsh.
To update your account to use zsh, please run `chsh -s /bin/zsh`.
For more details, please visit https://support.apple.com/kb/HT208050.
(base) Luiss-MacBook-Pro:\~ luiscunha$ sh teste2.sh
escolha uma opção

1) olá
2) adeus
   \#? 1
   \#? 2
   \#? olá
   \#? 1)
   \#? 1) olá
   \#?

Solution

  • Save this script to a file, for example my_menu.sh, and then run it using bash my_menu.sh.

    check this out :

    #!/bin/bash
    
    options=("olá" "adeus")
    PS3="Escolha uma opção: "
    
    select opt in "${options[@]}"; do
        case $REPLY in
            1)
                echo "Olá de volta!"
                break
                ;;
            2)
                echo "Mal criado chunga!"
                break
                ;;
            *)
                echo "Opção inválida. Tente novamente."
                ;;
        esac
    done