Bash’s built‑in select is strictly a “type the number + Enter” interface—it doesn’t natively support moving a cursor with ↑/↓ arrows. If you want an arrow‑key menu you have three main approaches:
- Pull in a helper (dialog, whiptail, fzf…)
- Use Readline or ANSI‑escape hacks yourself in pure Bash
- Drop to a full curses/TUI library (ncurses via Python, C, etc.)
Below are examples of (1) and (2).
1) Use an external menu helper
whiptail / dialog
#!/usr/bin/env bash
CHOICE=$(whiptail --title "Demo Menu" \
--menu "Select an option with ↑/↓ and Enter" 15 50 4 \
"1" "Create backup" \
"2" "Restore backup" \
"3" "Quit" 3>&1 1>&2 2>&3)
# whiptail returns the tag (e.g. "1") on stdout
case "$CHOICE" in
1) echo "Backing up…";;
2) echo "Restoring…";;
3) echo "Goodbye!"; exit 0;;
*) echo "Cancelled"; exit 1;;
esac
fzf
options=("Create" "Restore" "Quit")
selection=$(printf "%s\n" "${options[@]}" \
| fzf --height 10 --border --prompt="Choose: ")
echo "You picked: $selection"
2) DIY in pure Bash (read + ANSI codes)
#!/usr/bin/env bash
options=("Option A" "Option B" "Option C" "Quit")
pos=0
draw() {
clear
echo "Use ↑/↓ arrows and Enter to select:"
for i in "${!options[@]}"; do
if (( i == pos )); then
echo -e "\e[7m ${options[i]} \e[0m" # inverse video
else
echo " ${options[i]}"
fi
done
}
while true; do
draw
IFS= read -rsn1 key
if [[ $key == $'\x1b' ]]; then # beginning of an escape sequence
read -rsn2 -t 0.1 rest # read the next two chars
key+="$rest"
case $key in
$'\x1b[A') (( pos>0 )) && ((pos--)) ;; # up
$'\x1b[B') (( pos<${#options[@]}-1 )) && ((pos++)) ;; # down
esac
elif [[ $key == "" ]]; then # Enter key
echo "You chose: ${options[pos]}"
break
fi
done
How it works:
- Read one char (
-n1) quietly (-s).
- Arrow keys send a 3‑byte sequence:
ESC [ A (up) or ESC [ B (down).
- We detect & adjust
pos, redraw with tput/ANSI, and on Enter (key=="") we fire the selection.
TL;DR
- select → only number + Enter
- faster path → use
dialog/whiptail/fzf
- full‑custom → capture arrows via
read -rsnX + ANSI escape sequences