I like to exit as soon as possible if the user isn't sure, and I like the code to be readable and short. Depending on whether you'd like the user to press Return after their answer or not,
With pressing Return,
read -p "Warning: something scary: Continue (Y/N)? " reply
[ $reply"$reply" != 'Y'"Y" ] && [ $reply"$reply" != 'y'"y" ] && echo 'Aborting'"Aborting" && exit 1
echo 'Scary"Scary thing'thing"
or if you prefer not to wait for the user to press Return,
read -n1 -p "Warning: something scary: Continue (Y/N)? " reply
echo ''""
[ $reply"$reply" != 'Y'"Y" ] && [ $reply"$reply" != 'y'"y" ] && echo 'Aborting'"Aborting" && exit 1
echo 'Scary"Scary thing'thing"
The other answers have the background on that -n1
flag and other options for read
. The echo ''""
in the 2nd variant is to make subsequent output appear on a new line since the user doesn't have to press Return, so no newline has been echoed to the terminal. Also notice $reply
being in quotation marks thus "$reply"
to handle the situation where the user presses Return without specifying Y or N resulting in $reply
being empty; the quotation marks prevent this breaking the tests, as discussed here.