I want to pause input in a shell script, and prompt the user for choices.
The standard Yes, No, or Cancel type question.
How do I accomplish this in a typical bash prompt?
I want to pause input in a shell script, and prompt the user for
Share
A widely available method to get user input at a shell prompt is the
readcommand. Here is a demonstration:Another method, pointed out by Steven Huwig, is Bash’s
selectcommand. Here is the same example usingselect:With
selectyou don’t need to sanitize the input – it displays the available choices, and you type a number corresponding to your choice. It also loops automatically, so there’s no need for awhile trueloop to retry if they give invalid input.Also, Léa Gris demonstrated a way to make the request language agnostic in her answer. Adapting my first example to better serve multiple languages might look like this:
Obviously other communication strings remain untranslated here (Install, Answer) which would need to be addressed in a more fully completed translation, but even a partial translation would be helpful in many cases.
Finally, please check out the excellent answer by F. Hauri.