Skip to main content
Incorporated a recent edit using my own wording.
Source Link
Myrddin Emrys
  • 44.4k
  • 12
  • 44
  • 60

A widely available method to get user input at a shell prompt is the read command. Here is a demonstration:

while true; do read -p "Do you wish to install this program? " yn case $yn in [Yy]* ) make install; break;; [Nn]* ) exit;; * ) echo "Please answer yes or no.";; esac done 

Another method, pointed out by Steven Huwig, is Bash's select command. Here is the same example using select:

echo "Do you wish to install this program?" select yn in "Yes" "No"; do case $yn in Yes ) make install; break;; No ) exit;; esac done 

With select you 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 a while true loop to retry if they give invalid input.

The above example accepts only numeric responses. select is very strict in this regard. Below is a variation of select that is If you want to allow more forgivingflexible input (falling back toaccepting the user's raw $REPLY inwords of the event thatoptions, rather than select rejects their non-numeric responsejust their number), you can alter it like this:

echo "Do you wish to install this program?" select strictreply in "Yes" "No"; do relaxedreply=${strictreply:-$REPLY} case $relaxedreply in Yes | yes | y ) make install; break;; No | no | n ) exit;; esac done 

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:

set -- $(locale LC_MESSAGES) yesexpr="$1"; noexpr="$2"; yesword="$3"; noword="$4" while true; do read -p "Install (${yesword} / ${noword})? " yn if [[ "$yn" =~ $yesexpr ]]; then make install; exit; fi if [[ "$yn" =~ $noexpr ]]; then exit; fi echo "Answer ${yesword} / ${noword}." done 

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.

A widely available method to get user input at a shell prompt is the read command. Here is a demonstration:

while true; do read -p "Do you wish to install this program? " yn case $yn in [Yy]* ) make install; break;; [Nn]* ) exit;; * ) echo "Please answer yes or no.";; esac done 

Another method, pointed out by Steven Huwig, is Bash's select command. Here is the same example using select:

echo "Do you wish to install this program?" select yn in "Yes" "No"; do case $yn in Yes ) make install; break;; No ) exit;; esac done 

With select you 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 a while true loop to retry if they give invalid input.

The above example accepts only numeric responses. select is very strict in this regard. Below is a variation of select that is more forgiving (falling back to the user's raw $REPLY in the event that select rejects their non-numeric response):

echo "Do you wish to install this program?" select strictreply in "Yes" "No"; do relaxedreply=${strictreply:-$REPLY} case $relaxedreply in Yes | yes | y ) make install; break;; No | no | n ) exit;; esac done 

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:

set -- $(locale LC_MESSAGES) yesexpr="$1"; noexpr="$2"; yesword="$3"; noword="$4" while true; do read -p "Install (${yesword} / ${noword})? " yn if [[ "$yn" =~ $yesexpr ]]; then make install; exit; fi if [[ "$yn" =~ $noexpr ]]; then exit; fi echo "Answer ${yesword} / ${noword}." done 

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.

A widely available method to get user input at a shell prompt is the read command. Here is a demonstration:

while true; do read -p "Do you wish to install this program? " yn case $yn in [Yy]* ) make install; break;; [Nn]* ) exit;; * ) echo "Please answer yes or no.";; esac done 

Another method, pointed out by Steven Huwig, is Bash's select command. Here is the same example using select:

echo "Do you wish to install this program?" select yn in "Yes" "No"; do case $yn in Yes ) make install; break;; No ) exit;; esac done 

With select you 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 a while true loop to retry if they give invalid input. If you want to allow more flexible input (accepting the words of the options, rather than just their number), you can alter it like this:

echo "Do you wish to install this program?" select strictreply in "Yes" "No"; do relaxedreply=${strictreply:-$REPLY} case $relaxedreply in Yes | yes | y ) make install; break;; No | no | n ) exit;; esac done 

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:

set -- $(locale LC_MESSAGES) yesexpr="$1"; noexpr="$2"; yesword="$3"; noword="$4" while true; do read -p "Install (${yesword} / ${noword})? " yn if [[ "$yn" =~ $yesexpr ]]; then make install; exit; fi if [[ "$yn" =~ $noexpr ]]; then exit; fi echo "Answer ${yesword} / ${noword}." done 

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.

Provide a variation of `select` that accepts non-numeric replies.
Source Link

A widely available method to get user input at a shell prompt is the read command. Here is a demonstration:

while true; do read -p "Do you wish to install this program? " yn case $yn in [Yy]* ) make install; break;; [Nn]* ) exit;; * ) echo "Please answer yes or no.";; esac done 
 

Another method, pointed out by Steven Huwig, is Bash's select command. Here is the same example using select:

echo "Do you wish to install this program?" select yn in "Yes" "No"; do case $yn in Yes ) make install; break;; No ) exit;; esac done 

With select you 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 a while true loop to retry if they give invalid input.

The above example accepts only numeric responses. select is very strict in this regard. Below is a variation of select that is more forgiving (falling back to the user's raw $REPLY in the event that select rejects their non-numeric response):

echo "Do you wish to install this program?" select strictreply in "Yes" "No"; do relaxedreply=${strictreply:-$REPLY} case $relaxedreply in Yes | yes | y ) make install; break;; No | no | n ) exit;; esac done 

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:

set -- $(locale LC_MESSAGES) yesexpr="$1"; noexpr="$2"; yesword="$3"; noword="$4" while true; do read -p "Install (${yesword} / ${noword})? " yn if [[ "$yn" =~ $yesexpr ]]; then make install; exit; fi if [[ "$yn" =~ $noexpr ]]; then exit; fi echo "Answer ${yesword} / ${noword}." done 

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.

A widely available method to get user input at a shell prompt is the read command. Here is a demonstration:

while true; do read -p "Do you wish to install this program? " yn case $yn in [Yy]* ) make install; break;; [Nn]* ) exit;; * ) echo "Please answer yes or no.";; esac done 

Another method, pointed out by Steven Huwig, is Bash's select command. Here is the same example using select:

echo "Do you wish to install this program?" select yn in "Yes" "No"; do case $yn in Yes ) make install; break;; No ) exit;; esac done 

With select you 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 a while true loop 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:

set -- $(locale LC_MESSAGES) yesexpr="$1"; noexpr="$2"; yesword="$3"; noword="$4" while true; do read -p "Install (${yesword} / ${noword})? " yn if [[ "$yn" =~ $yesexpr ]]; then make install; exit; fi if [[ "$yn" =~ $noexpr ]]; then exit; fi echo "Answer ${yesword} / ${noword}." done 

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.

A widely available method to get user input at a shell prompt is the read command. Here is a demonstration:

while true; do read -p "Do you wish to install this program? " yn case $yn in [Yy]* ) make install; break;; [Nn]* ) exit;; * ) echo "Please answer yes or no.";; esac done 
 

Another method, pointed out by Steven Huwig, is Bash's select command. Here is the same example using select:

echo "Do you wish to install this program?" select yn in "Yes" "No"; do case $yn in Yes ) make install; break;; No ) exit;; esac done 

With select you 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 a while true loop to retry if they give invalid input.

The above example accepts only numeric responses. select is very strict in this regard. Below is a variation of select that is more forgiving (falling back to the user's raw $REPLY in the event that select rejects their non-numeric response):

echo "Do you wish to install this program?" select strictreply in "Yes" "No"; do relaxedreply=${strictreply:-$REPLY} case $relaxedreply in Yes | yes | y ) make install; break;; No | no | n ) exit;; esac done 

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:

set -- $(locale LC_MESSAGES) yesexpr="$1"; noexpr="$2"; yesword="$3"; noword="$4" while true; do read -p "Install (${yesword} / ${noword})? " yn if [[ "$yn" =~ $yesexpr ]]; then make install; exit; fi if [[ "$yn" =~ $noexpr ]]; then exit; fi echo "Answer ${yesword} / ${noword}." done 

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.

I accidentally down-voted, so I'm removing a few unnecessary words and upvoting.
Source Link
Aaron Newton
  • 2.3k
  • 1
  • 30
  • 35

The simplest and mostA widely available method to get user input at a shell prompt is the read command. The best way to illustrate its useHere is a simple demonstration:

while true; do read -p "Do you wish to install this program? " yn case $yn in [Yy]* ) make install; break;; [Nn]* ) exit;; * ) echo "Please answer yes or no.";; esac done 

Another method, pointed out by Steven Huwig, is Bash's select command. Here is the same example using select:

echo "Do you wish to install this program?" select yn in "Yes" "No"; do case $yn in Yes ) make install; break;; No ) exit;; esac done 

With select you 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 a while true loop 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:

set -- $(locale LC_MESSAGES) yesexpr="$1"; noexpr="$2"; yesword="$3"; noword="$4" while true; do read -p "Install (${yesword} / ${noword})? " yn if [[ "$yn" =~ $yesexpr ]]; then make install; exit; fi if [[ "$yn" =~ $noexpr ]]; then exit; fi echo "Answer ${yesword} / ${noword}." done 

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.

The simplest and most widely available method to get user input at a shell prompt is the read command. The best way to illustrate its use is a simple demonstration:

while true; do read -p "Do you wish to install this program? " yn case $yn in [Yy]* ) make install; break;; [Nn]* ) exit;; * ) echo "Please answer yes or no.";; esac done 

Another method, pointed out by Steven Huwig, is Bash's select command. Here is the same example using select:

echo "Do you wish to install this program?" select yn in "Yes" "No"; do case $yn in Yes ) make install; break;; No ) exit;; esac done 

With select you 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 a while true loop 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:

set -- $(locale LC_MESSAGES) yesexpr="$1"; noexpr="$2"; yesword="$3"; noword="$4" while true; do read -p "Install (${yesword} / ${noword})? " yn if [[ "$yn" =~ $yesexpr ]]; then make install; exit; fi if [[ "$yn" =~ $noexpr ]]; then exit; fi echo "Answer ${yesword} / ${noword}." done 

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.

A widely available method to get user input at a shell prompt is the read command. Here is a demonstration:

while true; do read -p "Do you wish to install this program? " yn case $yn in [Yy]* ) make install; break;; [Nn]* ) exit;; * ) echo "Please answer yes or no.";; esac done 

Another method, pointed out by Steven Huwig, is Bash's select command. Here is the same example using select:

echo "Do you wish to install this program?" select yn in "Yes" "No"; do case $yn in Yes ) make install; break;; No ) exit;; esac done 

With select you 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 a while true loop 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:

set -- $(locale LC_MESSAGES) yesexpr="$1"; noexpr="$2"; yesword="$3"; noword="$4" while true; do read -p "Install (${yesword} / ${noword})? " yn if [[ "$yn" =~ $yesexpr ]]; then make install; exit; fi if [[ "$yn" =~ $noexpr ]]; then exit; fi echo "Answer ${yesword} / ${noword}." done 

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.

fix mismatch between variable declared name and variable usage
Source Link
Loading
Add space for visually separating question from answer prompt
Source Link
Abdull
  • 28.2k
  • 27
  • 137
  • 185
Loading
Updated the locale aware version to work, per the updates to Léa Gris' answer.
Source Link
Myrddin Emrys
  • 44.4k
  • 12
  • 44
  • 60
Loading
Updated links to gnu.org to use https.
Source Link
Myrddin Emrys
  • 44.4k
  • 12
  • 44
  • 60
Loading
Stole more work from others (Léa Gris this time), but also improved crediting for my answers.
Source Link
Myrddin Emrys
  • 44.4k
  • 12
  • 44
  • 60
Loading
fixed links, adjusted formatting, adjusted phrasing
Source Link
User
  • 3k
  • 8
  • 29
  • 43
Loading
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot
Loading
Linked to good new answer.
Source Link
Myrddin Emrys
  • 44.4k
  • 12
  • 44
  • 60
Loading
Someone else suggested a rejected edit yesterday... but I tested it, and they are correct. My overrated answer has a syntax error. Fixed.
Source Link
Myrddin Emrys
  • 44.4k
  • 12
  • 44
  • 60
Loading
A comment pointed out that my `select` example had unnecessary complexity. I simplified it.
Source Link
Myrddin Emrys
  • 44.4k
  • 12
  • 44
  • 60
Loading
Update anchor on `read` URL.
Source Link
Christopher Orr
  • 111.9k
  • 27
  • 202
  • 196
Loading
Fixed to use read -p as suggested by Steve Baker.
Source Link
Myrddin Emrys
  • 44.4k
  • 12
  • 44
  • 60
Loading
Added Steven Huwig's answer, and hyperlinked to the instructions for 'read' and 'select'.
Source Link
Myrddin Emrys
  • 44.4k
  • 12
  • 44
  • 60
Loading
Source Link
Myrddin Emrys
  • 44.4k
  • 12
  • 44
  • 60
Loading