Classically (meaning it will work in POSIX-ish shells other than bash
), you'd write:
echo "Question? [Y/n]"
read choose
if [ "$choose" = "Y" ] || [ "$choose" = "y" ] || [ -z "$choose" ]
then
# code
fi
The quotes ensure that the test operator see an argument even if $choose
is an empty string.
The [[
operator seems to allow you to get away without quoting strings, but is not part of a POSIX shell. POSIX recognizes its existence by noting:
The following words may be recognized as reserved words on some implementations (when none of the characters are quoted), causing unspecified results:
[[ ]] function select
If you want the shell to leave the cursor on the same line as the prompt, you can use:
printf "Question? [Y/n] "
POSIX shells recognize the \c
escape; bash
does not:
echo "Question? [Y/n] \c"
(There may be a way to make bash
handle that, but printf
is probably more portable.)