Example usage that works on Ubuntu and other forms of Linux but not on Mac OS X.
while true; do
read -e -n 1 -p "Do you like Pie? > " -i y ANSWER
case $ANSWER in
[Nn]* ) exit;;
[Yy]* ) `./calculate-pi.sh`; break;;
* ) echo "Please answer y or n.";;
esac
done
Looks like the
-iflag was introduced with Bash 4.x. OS X still ships with Bash 3.2.48(1)-release. I don’t think there’s any way to preload the answer with text in Bash 3.x. Typically this is solved by treating the empty response as the default, and indicating this in the prompt (e.g."Do you like Pie? [Yn]> ").