Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This question already has an answer here:

Here is a portion of my script:

read main_menu

case "$main_menu" in

    "0" )   exit
            ;;
    "1" )   cp /etc/bamt/cgminer.conf.X11 /etc/bamt/cgminer.conf;
            sudo mine restart;
            ;;

How can I make it so after the user enters 0 or 1, 2 etc, he doesn't have to press the Enter key? Practically, when you enter the number it would directly jump to the next menu or function without the needing to press Enter.

Can you help me implement it here?

while :
do
    echo -e "\n Test script"
    echo -e "\t (0) Exit"
    echo -e "\t (1) Option 1"
    echo -n "Enter choice:"
      read main_menu
      case "$main_menu" in
         "0" ) exit
         ;;
         "1" ) exit
         ;;
esac
done
share|improve this question

marked as duplicate by l0b0, cuonglm, Ramesh, Anthon, slm Jul 15 '14 at 13:07

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer 1

up vote 6 down vote accepted

Bash read supports a number of options, among them -n 1:

$ read -n 1 main_menu
1$ echo $main_menu
1

The -n option means that:

read returns after reading nchars characters rather than waiting for a complete line of input, but honor a delimiter if fewer than nchars characters are read before the delimiter.

-n 1 tells read to return after reading a single character of input, so as soon as the user presses 1 then read will stop and save the input into the variable.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.