0

I built a tiny menu to use in a bash terminal with multiple options to select via number keys.

#!/bin/bash
PS3='Teleport to ... '
options=("→ option 1" "→ option 2" "Quit")
select opt in "${options[@]}"
do
    case $opt in
        "→ option 1")
            echo "option 1"
            break
            ;;
        "→ option 2")
            echo "option 2"
            break
            ;;
        "Quit")
            break
            ;;
        *) echo invalid option
            break
            ;;
    esac
done

At the moment I still need to confirm the selection by pressing enter. Is it possible to make the script respond to the input of the first pressed key directly?

2 Answers 2

2

read -n 1 reads one character. You cannot use select with it, though, so you have to write the while loop yourself.

Sign up to request clarification or add additional context in comments.

Comments

1

Yep, with bash (and not sh!) you can use something like:

_KEY=
read -d '' -sn1 _KEY

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.