Bash

Read Single Keystroke Without Enter

admin by @admin ADMIN
1h ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
For menus and confirm prompts where you want one-key response (no need to press Enter). -n 1 reads exactly one character, -s silences the echo.
Bash
Raw
read -n 1 -p "Continue? (y/n) " key
echo
[[ "$key" == "y" ]] && echo "Continuing…"

# Silent (no echo) — good for password-style entry
read -s -p "Password: " pw
echo
echo "Got ${#pw} chars"

# Read with a timeout (in seconds)
if read -t 5 -n 1 -p "Press any key (5s)… " key; then
    echo " got $key"
else
    echo " (timed out)"
fi
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.