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?
Linux - How to prompt for Yes/No/Cancel input in a Linux shell script
bashlinuxscriptingshell
Related Question
- Bash - How to check if a directory exists in a Bash shell script
- Bash - How to get the source directory of a Bash script from within the script itself
- Bash - How to iterate over arguments in a Bash script
- How to mkdir only if a directory does not already exist
- Bash - In the shell, what does ” 2>&1 ” mean
- Bash - YYYY-MM-DD format date in shell script
- Linux - Looping through the content of a file in Bash
- Bash - Check existence of input argument in a Bash shell script
- Linux - How to find all files containing specific text on Linux
- Bash - How to pause the shell script for a second before continuing
Best Solution
The simplest and most widely available method to get user input at a shell prompt is the
readcommand. The best way to illustrate its use is a simple 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.