Some may have found out that in Ubuntu when you install or update via terminal
you get a question similar to
“do you with to install or remove package [Y/n]?”
when you press enter its like its the same as when you type “Y” or “y”
So i was wondering how to make that in bash
This is what i have so far?
echo "question?[Y/n]"
read choose
if [ $choose ~= "Y" ] [ $choose ~= "y" ] [ $choose ~= "" ]
then
#code
fi
Classically (meaning it will work in POSIX-ish shells other than
bash), you’d write:The quotes ensure that the test operator see an argument even if
$chooseis 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:If you want the shell to leave the cursor on the same line as the prompt, you can use:
POSIX shells recognize the
\cescape;bashdoes not:(There may be a way to make
bashhandle that, butprintfis probably more portable.)