In this short article, we want to show how to handle pressed key without waiting to rerun key / enter key confirmation under Bash.
read -r -s -n 1 input echo "$input"
-rdisables waiting to type new line character to end read command (Return / Enter key is not required),
-sdoes not display typed characters,
-n 1expects to type one character.
Alternatively you can use short version too:
read -rsn1 input
In this section, we want to show how to create a simple menu that doesn't require to pres Return / Enter key after the option is typed.
#!/bin/bash echo '1. Message' echo '2. User' echo '3. Exit' echo echo 'Select one option [1-3]' echo while true; do read -r -s -n 1 input case "$input" in "1") echo "Selected 1 [1. Message]" ;; "2") echo "Selected 2 [2. User]" ;; "3") echo "Selected 3 [3. Exit]" exit 0 ;; esac done