79827353

Date: 2025-11-22 13:56:39
Score: 1.5
Natty:
Report link

POSIX shell menu

Here is how I will do this:

No, clear won't affect your input!

#!/bin/sh

number=-1
while true; do
    clear
    echo "------ M E N U ------"
    echo "1 - Primeira opção"
    echo "2 - Segunda opção"
    echo "3 - Terceira opção"
    echo "0 - Sair"

    while true; do
        printf 'Opção: '
        read -r number
        case $number in
                ''|*[!0-9]* )
                        echo "Input '$number' is not a number."
                        ;;
                * )
                        break
                        ;;
        esac
    done
    case $number in
            0 | 1 | 2 | 3 ) break ;;
    esac
    echo "Input $number is not in menu!"
    sleep 1.5 # time to read previous message before next `clear`.
done
echo "Você escolheu a opção $number"        

POSIX shell menu, using dialog or equivalents

From How do I prompt for Yes/No/Cancel input in a Linux shell script?

#!/bin/sh

number="$(
     dialog --menu 'Menu' 20 60 6 1 "Primeira opção" 2 "Segunda opção" \
         3 "Terceira opção" 0 "Sair" 2>&1 >/dev/tty
)"
echo "Você escolheu a opção $number"

dialog menu

POSIX shell menu, using fzf

Or using recent fzf utility:

#!/bin/sh

number="$(
  printf '0 - Sair\n3 - Terceira opção\n2 - Segunda opção\n1 - Primeira opção'|
      fzf)"

echo "Você escolheu a opção ${number%% *}"

enter image description here

Reasons:
  • Blacklisted phrase (1): How do I
  • Blacklisted phrase (3): Você
  • Probably link only (1):
  • Long answer (-1):
  • Has code block (-0.5):
  • High reputation (-2):
Posted by: F. Hauri - Give Up GitHub