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?

- 397
- 4
- 11

- 42,126
- 11
- 38
- 51
-
38Just as a note: convention for prompts are such that if you present a `[yn]` option, the one that is capitalized is default, i.e. `[Yn]` defaults to "yes", and `[yN]` defaults to "no". See https://ux.stackexchange.com/a/40445/43532 – Tyzoid May 30 '19 at 19:46
-
2Anyone coming here from ZSH, see [this answer](https://stackoverflow.com/a/15175186/2089675) for how to use the `read` command to prompt – smac89 Dec 20 '19 at 04:34
-
1You can also consider [my related Q/A on U&L.SE](https://unix.stackexchange.com/q/630788/318461) about the canonical way to pause in `bash`. The provided results could easily be transferred. – Cadoiz Oct 15 '21 at 08:57
39 Answers
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.

- 327
- 3
- 6

- 42,126
- 11
- 38
- 51
-
40Using Bash in OS X Leopard, I changed `exit` to `break` to keep from closing the tab when I selected 'no'. – Trey Piepmeier Dec 02 '09 at 18:34
-
1How does this work with options longer than Yes or No? In the case clause, do you write something like: Install program and do nothing afterwards ) make install; break; – Shawn Feb 29 '12 at 06:12
-
1@Shawn Using **read** you can, of course, use any glob or regex pattern supported by the bash shell's switch statement. It just matches the text typed to your patterns until it finds a match. Using **select**, the list of choices is given to the command and it displays them to the user. You can have the items in the list be as long or as short as you like. I recommand checking their man pages for comprehensive usage information. – Myrddin Emrys Feb 29 '12 at 21:45
-
4
-
You know, it's probably not necessary. And you are the first person to notice this in six years. :-) I was following the example in the [Bash Beginner's Guide](http://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_09_06.html), but that example breaks because there are multiple values provided by `select`, not due to [switch statement fallthough](http://en.wikipedia.org/wiki/Switch_statement#Fallthrough) like I thought. – Myrddin Emrys Jun 16 '14 at 21:56
-
@MyrddinEmrys, I still see the `break` in your example. At least the version of bash (4.2.53) I'm using is looping the select forever until `break`. Try with this command: `select result in asd dsa fds; do echo $result; done` – akostadinov Nov 07 '14 at 08:29
-
1FWIW, I used this example to create a script that I intended to trigger via a remote SSH session. My SSH command looked like this: `ssh my-server 'path/to/myscript.sh'`. When executing this way, the prompt text for the `read -p` command does not show up. However, output from the `echo` command does. So for me, the better solution was to use `echo -n "Do something? "` followed by `read yn`. – Travesty3 Jan 04 '18 at 15:08
-
This will have issues with `command | myscript.sh` or `myscript.sh | command`. See [this answer](https://stackoverflow.com/a/51078339/5353461) for a solution. – Tom Hale Jun 28 '18 at 08:41
-
1Maybe you could also mention that hard-coded RegEx character classes could be replaced by locale aware RegEx like this: `read -r -d '' yesexpr noexpr yesstr nostr messages_codeset < <(locale LC_MESSAGES)`; then respectively use `$yesexpr` and `$noexpr` rather than hard-coded RegEx. – Léa Gris Aug 31 '19 at 13:31
-
In the last example from Léa Gris, the `LC_MESSAGES` regex is set to `yesptrn` and `noptrn` but `yesexpr` and `noexpr` are used inside the `if` conditions which causes the conditions to fail because those variables hold no value – RisingSun Sep 29 '21 at 19:41
-
Just a hint: pay attention to really use `bash`, not `sh` like described [in this answer on U&L.SE](https://unix.stackexchange.com/a/630792/318461). – Cadoiz Oct 15 '21 at 09:10
At least five answers for one generic question.
Depending on
- posix compliant: could work on poor systems with generic shell environments
- bash specific: using so called bashisms
and if you want
- simple 'in line'' question / answer (generic solutions)
- pretty formatted interfaces, like ncurses or more graphical using libgtk or libqt...
- use powerful readline history capability
1. POSIX generic solutions
You could use the read
command, followed by if ... then ... else
:
printf 'Is this a good question (y/n)? '
read answer
if [ "$answer" != "${answer#[Yy]}" ] ;then
echo Yes
else
echo No
fi
(Thanks to Adam Katz's comment: Replaced the test with the new one above that's more portable and avoids one fork :)
POSIX, but single key feature
But if you don't want the user to have to hit Return, you could write:
(Edited: As @JonathanLeffler rightly suggest, saving stty's configuration could be better than simply force them to sane.)
printf 'Is this a good question (y/n)? '
old_stty_cfg=$(stty -g)
stty raw -echo ; answer=$(head -c 1) ; stty $old_stty_cfg # Careful playing with stty
if [ "$answer" != "${answer#[Yy]}" ];then
echo Yes
else
echo No
fi
Note: This was tested under sh, bash, ksh, dash and busybox!
Same, but waiting explicitly for y or n:
#/bin/sh
printf 'Is this a good question (y/n)? '
old_stty_cfg=$(stty -g)
stty raw -echo
answer=$( while ! head -c 1 | grep -i '[ny]' ;do true ;done )
stty $old_stty_cfg
if [ "$answer" != "${answer#[Yy]}" ];then
echo Yes
else
echo No
fi
Using dedicated tools
There are many tools which were built using libncurses
, libgtk
, libqt
or other graphical libraries. For example, using whiptail
:
if whiptail --yesno "Is this a good question" 20 60 ;then
echo Yes
else
echo No
fi
Depending on your system, you may need to replace whiptail
with another similiar tool:
dialog --yesno "Is this a good question" 20 60 && echo Yes
gdialog --yesno "Is this a good question" 20 60 && echo Yes
kdialog --yesno "Is this a good question" 20 60 && echo Yes
where 20
is height of dialog box in number of lines and 60
is width of the dialog box. These tools all have near same syntax.
DIALOG=whiptail
if [ -x /usr/bin/gdialog ] ;then DIALOG=gdialog ; fi
if [ -x /usr/bin/xdialog ] ;then DIALOG=xdialog ; fi
...
$DIALOG --yesno ...
2. Bash specific solutions
Basic in line method
read -p "Is this a good question (y/n)? " answer
case ${answer:0:1} in
y|Y )
echo Yes
;;
* )
echo No
;;
esac
I prefer to use case
so I could even test for yes | ja | si | oui
if needed...
in line with single key feature
Under bash, we can specify the length of intended input for for the read
command:
read -n 1 -p "Is this a good question (y/n)? " answer
Under bash, read
command accepts a timeout parameter, which could be useful.
read -t 3 -n 1 -p "Is this a good question (Y/n)? " answer
[ -z "$answer" ] && answer="Yes" # if 'yes' have to be default choice
Timeout with countdown:
i=6 ;while ((i-->1)) &&
! read -sn 1 -t 1 -p $'\rIs this a good question (Y/n)? '$i$'..\e[3D' answer;do
:;done ;[[ $answer == [nN] ]] && answer=No || answer=Yes ;echo "$answer "
3. Some tricks for dedicated tools
More sophisticated dialog boxes, beyond simple yes - no
purposes:
dialog --menu "Is this a good question" 20 60 12 y Yes n No m Maybe
Progress bar:
dialog --gauge "Filling the tank" 20 60 0 < <(
for i in {1..100};do
printf "XXX\n%d\n%(%a %b %T)T progress: %d\nXXX\n" $i -1 $i
sleep .033
done
)
Little demo:
#!/bin/sh
while true ;do
[ -x "$(which ${DIALOG%% *})" ] || DIALOG=dialog
DIALOG=$($DIALOG --menu "Which tool for next run?" 20 60 12 2>&1 \
whiptail "dialog boxes from shell scripts" >/dev/tty \
dialog "dialog boxes from shell with ncurses" \
gdialog "dialog boxes from shell with Gtk" \
kdialog "dialog boxes from shell with Kde" ) || break
clear;echo "Choosed: $DIALOG."
for i in `seq 1 100`;do
date +"`printf "XXX\n%d\n%%a %%b %%T progress: %d\nXXX\n" $i $i`"
sleep .0125
done | $DIALOG --gauge "Filling the tank" 20 60 0
$DIALOG --infobox "This is a simple info box\n\nNo action required" 20 60
sleep 3
if $DIALOG --yesno "Do you like this demo?" 20 60 ;then
AnsYesNo=Yes; else AnsYesNo=No; fi
AnsInput=$($DIALOG --inputbox "A text:" 20 60 "Text here..." 2>&1 >/dev/tty)
AnsPass=$($DIALOG --passwordbox "A secret:" 20 60 "First..." 2>&1 >/dev/tty)
$DIALOG --textbox /etc/motd 20 60
AnsCkLst=$($DIALOG --checklist "Check some..." 20 60 12 \
Correct "This demo is useful" off \
Fun "This demo is nice" off \
Strong "This demo is complex" on 2>&1 >/dev/tty)
AnsRadio=$($DIALOG --radiolist "I will:" 20 60 12 \
" -1" "Downgrade this answer" off \
" 0" "Not do anything" on \
" +1" "Upgrade this anser" off 2>&1 >/dev/tty)
out="Your answers:\nLike: $AnsYesNo\nInput: $AnsInput\nSecret: $AnsPass"
$DIALOG --msgbox "$out\nAttribs: $AnsCkLst\nNote: $AnsRadio" 20 60
done
More samples? Have a look at Using whiptail for choosing USB device and USB removable storage selector: USBKeyChooser
5. Using readline's history
Example:
#!/bin/bash
set -i
HISTFILE=~/.myscript.history
history -c
history -r
myread() {
read -e -p '> ' $1
history -s ${!1}
}
trap 'history -a;exit' 0 1 2 3 6
while myread line;do
case ${line%% *} in
exit ) break ;;
* ) echo "Doing something with '$line'" ;;
esac
done
This will create a file .myscript.history
in your $HOME
directory, than you could use readline's history commands, like Up, Down, Ctrl+r and others.

- 64,122
- 17
- 116
- 137
-
6Note that `stty` provides the `-g` option for use: `old_stty=$(stty -g); stty raw -echo; …; stty "$old_stty"`. This restores the setting exactly as they were found, which may or may not be the same as `stty -sane`. – Jonathan Leffler Feb 07 '15 at 05:09
-
6`read answer` will interpret backslashes before spaces and line feeds, and otherwise strip them which is rarely intended. Use `read -r answer` instead as per [SC2162](https://github.com/koalaman/shellcheck/wiki/SC2162). – vlfig Mar 31 '17 at 14:03
-
@vlfig A agree: expecting backslashes from user input in a *Yes/No/Cancel* dialog is ***rarely intended***... I think It's the main reason because I've not used `-r` option. – F. Hauri - Give Up GitHub Mar 31 '17 at 16:17
-
1The "Using readline's history" method is so terribly inappropriate for the OP's question. I'm glad you included it anyway. I have dozens of much more complicated scripts that I intend to update with that pattern! – Bruno Bronosky Oct 19 '17 at 04:18
-
7You can use `case` for POSIX as well as bash (use a wildcard condition rather than a bash substring: `case $answer in; [Yy]* ) echo Yes ;;`), but I prefer using a conditional statement instead, favoring `[ "$answer" != "${answer#[Yy]}" ]` over your `echo "$answer" | grep -iq ^y`. It's more portable (some non-GNU greps don't implement `-q` correctly) and it doesn't have the system call. `${answer#[Yy]}` uses parameter expansion to remove `Y` or `y` from the beginning of `$answer`, causing an inequality when either is present. This works in any POSIX shell (dash, ksh, bash, zsh, busybox, etc). – Adam Katz Apr 10 '18 at 16:44
-
1
-
3@CarterPape Yes, this was a joke! But in this detalled answer, you may find a lot of tips (second point present at least 3 different methods)! And ... from approx 5 years now, you are first to tell about my counting method! ;-)) – F. Hauri - Give Up GitHub Apr 01 '20 at 20:09
-
1Use `printf %s` instead of `echo -n` in POSIX. [According to POSIX.1-2017](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html), `echo` doesn't recognize any flags. – snath03 May 25 '22 at 05:32
echo "Please enter some input: "
read input_variable
echo "You entered: $input_variable"

- 23,070
- 14
- 64
- 77
-
35I disagree, because it only implements a portion of the functionality of the 'Yes, No, Cancel' dialog in DOS. The part it fails to implement is input checking... looping until a valid answer is received. – Myrddin Emrys Sep 23 '16 at 15:49
-
4(The original question title was "How do I prompt for input in a Linux shell script?") – Pistos Jul 09 '17 at 16:53
-
8But the original question description is unchanged, and always asked for a response to a Yes/No/Cancel prompt. The title has been updated to be clearer than my original one, but the question description was always clear (in my opinion). – Myrddin Emrys Sep 06 '17 at 13:52
You can use the built-in read command ; Use the -p
option to prompt the user with a question.
Since BASH4, you can now use -i
to suggest an answer :
read -e -p "Enter the path to the file: " -i "/usr/local/etc/" FILEPATH
echo $FILEPATH
(But remember to use the "readline" option -e
to allow line editing with arrow keys)
If you want a "yes / no" logic, you can do something like this:
read -e -p "
List the content of your home dir ? [Y/n] " YN
[[ $YN == "y" || $YN == "Y" || $YN == "" ]] && ls -la ~/

- 8,049
- 4
- 57
- 83
-
6It should be noted that `FILEPATH` is the variable name you have chosen, and is set with the answer to the command prompt. So if you were to then run `vlc "$FILEPATH"`, for example, `vlc` would open that file. – Ken Sharp Feb 23 '15 at 01:45
-
-
-
1Without the `-e` flag/option, you might (depending on the implementation) not be able to type "y", and then change your mind and replace it with a "n" (or anything else for that matter) ; When documenting a command, listing the options separately is better for readability/clarity, among other reasons. – yPhil Nov 13 '18 at 11:16
-
Just a hint: pay attention to really use `bash`, not `sh` like described in [this answer on U&L.SE](https://unix.stackexchange.com/a/630792/318461). – Cadoiz Oct 15 '21 at 09:46
Bash has select for this purpose. Here's how you would use it in a script:
select result in Yes No Cancel
do
echo $result
done
This is what it would look like to use:
$ bash examplescript.sh
1) Yes
2) No
3) Cancel
#? 1
Yes
#? 2
No
#? 3
Cancel
#?

- 136,138
- 45
- 251
- 267

- 20,015
- 9
- 55
- 79
-
24+1 Geniously simple solution. Only thing: This will prompt and promt and prompt... until you add an `exit` inside :) – kaiser Feb 28 '13 at 00:24
-
7(kaiser: To break from it, just enter the EOT: `Ctrl-D`. But of course, real code using it will need a break or an exit in the body.) – Zorawar Apr 22 '14 at 22:50
-
19This will not allow you to enter y or n, though.You choose by entering 1 2 or 3. – djjeck Sep 10 '14 at 16:03
-
8`exit` will exit the script all together, `break` will only exit the loop you are in (if you are on a `while` or `case` loop) – wranvaud Nov 06 '16 at 17:48
read -p "Are you alright? (y/n) " RESP
if [ "$RESP" = "y" ]; then
echo "Glad to hear it"
else
echo "You need more bash programming"
fi

- 659
- 5
- 2
inquire () {
echo -n "$1 [y/n]? "
read answer
finish="-1"
while [ "$finish" = '-1' ]
do
finish="1"
if [ "$answer" = '' ];
then
answer=""
else
case $answer in
y | Y | yes | YES ) answer="y";;
n | N | no | NO ) answer="n";;
*) finish="-1";
echo -n 'Invalid response -- please reenter:';
read answer;;
esac
fi
done
}
... other stuff
inquire "Install now?"
...

- 46,058
- 19
- 106
- 116

- 904
- 5
- 5
-
2Put four spaces in the beginning of each line to preserve the formatting of code. – Jouni K. Seppänen Oct 22 '08 at 17:15
-
10Why we providing 'y' and 'n' as parameters to inquire() if the case switches are hardcoded? That's just asking for misuse. They are fixed parameters, not changable, so the echo on line 2 should read: echo -n "$1 [Y/N]? " They can't be changed, so they shouldn't be supplied. – Myrddin Emrys Oct 22 '08 at 20:06
-
1@MyrddinEmrys Could you please elaborate your comment? Or post a link to an article or a couple of keywords so I could do research on my own. – Mateusz Piotrowski Mar 26 '16 at 20:46
-
4@MateuszPiotrowski The answer has been edited and improved since I made my comment. You can click the 'edited Dec 23' link above to view all the past versions of this answer. Back in 2008, the code was quite different. – Myrddin Emrys Mar 27 '16 at 05:21
Here's something I put together:
#!/bin/sh
promptyn () {
while true; do
read -p "$1 " yn
case $yn in
[Yy]* ) return 0;;
[Nn]* ) return 1;;
* ) echo "Please answer yes or no.";;
esac
done
}
if promptyn "is the sky blue?"; then
echo "yes"
else
echo "no"
fi
I'm a beginner, so take this with a grain of salt, but it seems to work.

- 272,448
- 266
- 850
- 1,236
-
9If you change `case $yn in` to `case ${yn:-$2} in` then you can use the second argument as the default value, Y or N. – jchook Jul 24 '13 at 20:21
-
1
-
It's nice that your code works directly with `if` statements! But it doesn't seem to work in scripts where `set -e` is used. Do you got any workaround for that? – winklerrr Dec 09 '20 at 08:15
-
@winklerrr You'll have to elaborate. Seems to work fine with `set -e`. You sure you don't have an error somewhere else in your script? – mpen Dec 09 '20 at 09:01
-
In my [solution](https://stackoverflow.com/questions/65213539/bash-prompt-with-yes-no-and-cancel) I'm echoing the answer yes or no directly in the function which I tried to capture with a subshell like so: `answer="$(promptyn "is the sky blue?")"`. I think that led to the "bug". – winklerrr Dec 09 '20 at 09:40
You want:
- Bash builtin commands (i.e. portable)
- Check TTY
- Default answer
- Timeout
- Colored question
Snippet
do_xxxx=y # In batch mode => Default is Yes
[[ -t 0 ]] && # If TTY => Prompt the question
read -n 1 -p $'\e[1;32m
Do xxxx? (Y/n)\e[0m ' do_xxxx # Store the answer in $do_xxxx
if [[ $do_xxxx =~ ^(y|Y|)$ ]] # Do if 'y' or 'Y' or empty
then
xxxx
fi
Explanations
[[ -t 0 ]] && read ...
=> Call commandread
if TTYread -n 1
=> Wait for one character$'\e[1;32m ... \e[0m '
=> Print in green
(green is fine because readable on both white/black backgrounds)[[ $do_xxxx =~ ^(y|Y|)$ ]]
=> bash regex
Timeout => Default answer is No
do_xxxx=y
[[ -t 0 ]] && { # Timeout 5 seconds (read -t 5)
read -t 5 -n 1 -p $'\e[1;32m
Do xxxx? (Y/n)\e[0m ' do_xxxx || # read 'fails' on timeout
do_xxxx=n ; } # Timeout => answer No
if [[ $do_xxxx =~ ^(y|Y|)$ ]]
then
xxxx
fi

- 51,447
- 27
- 165
- 200
-
Seems like there is typo: > if [[ $do_xxxx =~ ^(y|Y|)$ ]] Should be: `if [[ $do_xxxx =~ ^(y|Y)$ ]]` – Vladimir Yakovenko Oct 19 '22 at 10:32
The easiest way to achieve this with the least number of lines is as follows:
read -p "<Your Friendly Message here> : y/n/cancel" CONDITION;
if [ "$CONDITION" == "y" ]; then
# do something here!
fi
The if
is just an example: it is up to you how to handle this variable.

- 15,395
- 32
- 113
- 196

- 2,310
- 1
- 21
- 29
Use the read
command:
echo Would you like to install? "(Y or N)"
read x
# now check if $x is "y"
if [ "$x" = "y" ]; then
# do something here!
fi
and then all of the other stuff you need

- 15,395
- 32
- 113
- 196

- 239
- 2
- 2
This solution reads a single character and calls a function on a yes response.
read -p "Are you sure? (y/n) " -n 1
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
do_something
fi

- 56,821
- 26
- 143
- 139
-
2@Jav the echo prints a newline after your response. Without it, the next thing to be printed would appear immediately after your response on the same line. Try removing the `echo` to see for yourself. – Dennis Apr 01 '14 at 16:10
To get a nice ncurses-like inputbox use the command dialog like this:
#!/bin/bash
if (dialog --title "Message" --yesno "Want to do something risky?" 6 25)
# message box will have the size 25x6 characters
then
echo "Let's do something risky"
# do something risky
else
echo "Let's stay boring"
fi
The dialog package is installed by default at least with SUSE Linux. Looks like:

- 1,114
- 9
- 21
-
There is also a `--defaultno` argument to ensure the "No" option is selected by default. – WiMantis Jul 29 '21 at 13:15
It is possible to handle a locale-aware "Yes / No choice" in a POSIX shell; by using the entries of the LC_MESSAGES
locale category, witch provides ready-made RegEx patterns to match an input, and strings for localized Yes No.
#!/usr/bin/env sh
# Getting LC_MESSAGES values into variables
# shellcheck disable=SC2046 # Intended IFS splitting
IFS='
' set -- $(locale LC_MESSAGES)
yesexpr="$1"
noexpr="$2"
yesstr="$3"
nostr="$4"
messages_codeset="$5" # unused here, but kept as documentation
# Display Yes / No ? prompt into locale
echo "$yesstr / $nostr ?"
# Read answer
read -r yn
# Test answer
case "$yn" in
# match only work with the character class from the expression
${yesexpr##^}) echo "answer $yesstr" ;;
${noexpr##^}) echo "answer $nostr" ;;
esac
EDIT: As @Urhixidur mentioned in his comment:
Unfortunately, POSIX only specifies the first two (yesexpr and noexpr). On Ubuntu 16, yesstr and nostr are empty.
See: https://www.ee.ryerson.ca/~courses/ele709/susv4/xrat/V4_xbd_chap07.html#tag_21_07_03_06
LC_MESSAGES
The
yesstr
andnostr
locale keywords and theYESSTR
andNOSTR
langinfo items were formerly used to match user affirmative and negative responses. In POSIX.1-2008, theyesexpr
,noexpr
,YESEXPR
, andNOEXPR
extended regular expressions have replaced them. Applications should use the general locale-based messaging facilities to issue prompting messages which include sample desired responses.
Alternatively using locales the Bash way:
#!/usr/bin/env bash
IFS=$'\n' read -r -d '' yesexpr noexpr _ < <(locale LC_MESSAGES)
printf -v yes_or_no_regex "(%s)|(%s)" "$yesexpr" "$noexpr"
printf -v prompt $"Please answer Yes (%s) or No (%s): " "$yesexpr" "$noexpr"
declare -- answer=;
until [[ "$answer" =~ $yes_or_no_regex ]]; do
read -rp "$prompt" answer
done
if [[ -n "${BASH_REMATCH[1]}" ]]; then
echo $"You answered: Yes"
else
echo $"No, was your answer."
fi
The answer is matched using locale environment's provided regexps.
To translate the remaining messages, use bash --dump-po-strings scriptname
to output the po strings for localization:
#: scriptname:8
msgid "Please answer Yes (%s) or No (%s): "
msgstr ""
#: scriptname:17
msgid "You answered: Yes"
msgstr ""
#: scriptname:19
msgid "No, was your answer."
msgstr ""

- 17,497
- 4
- 32
- 41
-
-
1Unfortunately, POSIX only specifies the first two (yesexpr and noexpr). On Ubuntu 16, yesstr and nostr are empty. – Urhixidur Jan 09 '20 at 15:26
-
2But wait! There's worse news! The bash case statement expressions are not regular, they are filename expressions. So Ubuntu 16's yesexpr and noexpr ("^[yY].*" and "^[nN].*", respectively) will fail utterly because of the embedded period. In a regular expression, ".*" means "any non-newline character, zero or more times". But in a case statement, it's a literal "." followed by any number of characters. – Urhixidur Jan 09 '20 at 16:46
-
1Finally the best that can be done with `yesexpr` and `noexpr` in a shell environment, is use it in Bash's specific RegEx matching `if [[ "$yn" =~ $yesexpr ]]; then echo $"Answered yes"; else echo $"Answered no"; fi` – Léa Gris Jan 09 '20 at 17:17
-
I'm under SUSE Tumbleweed and `local LC_MESSAGES` displays `^[+1yY] ^[-0nN] yes no UTF-8`. It's interesting that these regular expressions accept +/- and 0/1. Maybe they expect a robot to use the prompt. – foxesque Jun 05 '23 at 12:09
In my case I needed to read from a downloaded script i.e.,
curl -Ss https://example.com/installer.sh | sh
The line read -r yn </dev/tty
allowed it to read input in this case.
printf "These files will be uploaded. Is this ok? (y/N) "
read -r yn </dev/tty
if [ "$yn" = "y" ]; then
# Yes
else
# No
fi

- 9,316
- 3
- 66
- 70

- 6,571
- 3
- 55
- 49
-
An important part of this is input validation. I think adapting my first example to accept `tty` input as you did would have done as well for you, and also gotten looping on bad input (imagine a few characters in the buffer; your method would force the user to **always** choose no). – Myrddin Emrys May 22 '14 at 13:03
Single keypress only
Here's a longer, but reusable and modular approach:
- Returns
0
=yes and1
=no - No pressing enter required - just a single character
- Can press enter to accept the default choice
- Can disable default choice to force a selection
- Works for both
zsh
andbash
.
Defaulting to "no" when pressing enter
Note that the N
is capitalsed. Here enter is pressed, accepting the default:
$ confirm "Show dangerous command" && echo "rm *"
Show dangerous command [y/N]?
Also note, that [y/N]?
was automatically appended.
The default "no" is accepted, so nothing is echoed.
Re-prompt until a valid response is given:
$ confirm "Show dangerous command" && echo "rm *"
Show dangerous command [y/N]? X
Show dangerous command [y/N]? y
rm *
Defaulting to "yes" when pressing enter
Note that the Y
is capitalised:
$ confirm_yes "Show dangerous command" && echo "rm *"
Show dangerous command [Y/n]?
rm *
Above, I just pressed enter, so the command ran.
No default on enter - require y
or n
$ get_yes_keypress "Here you cannot press enter. Do you like this [y/n]? "
Here you cannot press enter. Do you like this [y/n]? k
Here you cannot press enter. Do you like this [y/n]?
Here you cannot press enter. Do you like this [y/n]? n
$ echo $?
1
Here, 1
or false was returned. Note that with this lower-level function you'll need to provide your own [y/n]?
prompt.
Code
# Read a single char from /dev/tty, prompting with "$*"
# Note: pressing enter will return a null string. Perhaps a version terminated with X and then remove it in caller?
# See https://unix.stackexchange.com/a/367880/143394 for dealing with multi-byte, etc.
function get_keypress {
local REPLY IFS=
>/dev/tty printf '%s' "$*"
[[ $ZSH_VERSION ]] && read -rk1 # Use -u0 to read from STDIN
# See https://unix.stackexchange.com/q/383197/143394 regarding '\n' -> ''
[[ $BASH_VERSION ]] && </dev/tty read -rn1
printf '%s' "$REPLY"
}
# Get a y/n from the user, return yes=0, no=1 enter=$2
# Prompt using $1.
# If set, return $2 on pressing enter, useful for cancel or defualting
function get_yes_keypress {
local prompt="${1:-Are you sure [y/n]? }"
local enter_return=$2
local REPLY
# [[ ! $prompt ]] && prompt="[y/n]? "
while REPLY=$(get_keypress "$prompt"); do
[[ $REPLY ]] && printf '\n' # $REPLY blank if user presses enter
case "$REPLY" in
Y|y) return 0;;
N|n) return 1;;
'') [[ $enter_return ]] && return "$enter_return"
esac
done
}
# Credit: http://unix.stackexchange.com/a/14444/143394
# Prompt to confirm, defaulting to NO on <enter>
# Usage: confirm "Dangerous. Are you sure?" && rm *
function confirm {
local prompt="${*:-Are you sure} [y/N]? "
get_yes_keypress "$prompt" 1
}
# Prompt to confirm, defaulting to YES on <enter>
function confirm_yes {
local prompt="${*:-Are you sure} [Y/n]? "
get_yes_keypress "$prompt" 0
}

- 40,825
- 36
- 187
- 242
-
When I tested this script, instead of the outut above I got, ```Show dangerous command [y/N]? [y/n]?``` and ```Show dangerous command [Y/n]? [y/n]?``` – Ilias Karim Jun 30 '18 at 14:15
-
You can use the default REPLY
on a read
, convert to lowercase and compare to a set of variables with an expression.
The script also supports ja
/si
/oui
read -rp "Do you want a demo? [y/n/c] "
[[ ${REPLY,,} =~ ^(c|cancel)$ ]] && { echo "Selected Cancel"; exit 1; }
if [[ ${REPLY,,} =~ ^(y|yes|j|ja|s|si|o|oui)$ ]]; then
echo "Positive"
fi

- 19,067
- 2
- 23
- 43
read -e -p "Enter your choice: " choice
The -e
option enables the user to edit the input using arrow keys.
If you want to use a suggestion as input:
read -e -i "yes" -p "Enter your choice: " choice
-i
option prints a suggestive input.

- 21,542
- 10
- 90
- 108
-
yap, `-e` `-i` don't work in sh (Bourne shell), but the question is tagged bash specifically.. – Jahid May 10 '15 at 20:22
Lots of good answers to this question, but from what I can see none of them are my ideal, which would:
- Be simple, just a couple lines of shell
- Work with a single y/n keypress (no need to press enter)
- Default to yes if you just hit enter
- Work with an uppercase Y/N as well
Here's my version which does has those properties:
read -n1 -p "Continue? (Y/n) " confirm
if ! echo $confirm | grep '^[Yy]\?$'; then
exit 1
fi
You can modify that conditional to only run on "yes" (just remove the !
in the if
statement) or add an else
if you want to run code on both branches.

- 480
- 5
- 11
-
1Nicely done! Never too late to donate to an old question. I appreciate the single keypress (y, n, or enter) answer. I think you should include the else version, since my original question was for a full three branch question (if you count 'Cancel' as 'exit;;') – Myrddin Emrys Jun 21 '22 at 22:46
One-liner:
read -p "Continue? [Enter] → Yes, [Ctrl]+[C] → No."
This assumes that "No" and "Cancel" have the same outcome, so no reason to treat them differently.

- 4,132
- 1
- 26
- 29
I noticed that no one posted an answer showing multi-line echo menu for such simple user input so here is my go at it:
#!/bin/bash
function ask_user() {
echo -e "
#~~~~~~~~~~~~#
| 1.) Yes |
| 2.) No |
| 3.) Quit |
#~~~~~~~~~~~~#\n"
read -e -p "Select 1: " choice
if [ "$choice" == "1" ]; then
do_something
elif [ "$choice" == "2" ]; then
do_something_else
elif [ "$choice" == "3" ]; then
clear && exit 0
else
echo "Please select 1, 2, or 3." && sleep 3
clear && ask_user
fi
}
ask_user
This method was posted in the hopes that someone may find it useful and time-saving.

- 1,170
- 13
- 17
Check this
read -p "Continue? (y/n): " confirm && [[ $confirm == [yY] || $confirm == [yY][eE][sS] ]] || exit 1

- 1,733
- 1
- 16
- 19
Multiple choice version:
ask () { # $1=question $2=options
# set REPLY
# options: x=..|y=..
while $(true); do
printf '%s [%s] ' "$1" "$2"
stty cbreak
REPLY=$(dd if=/dev/tty bs=1 count=1 2> /dev/null)
stty -cbreak
test "$REPLY" != "$(printf '\n')" && printf '\n'
(
IFS='|'
for o in $2; do
if [ "$REPLY" = "${o%%=*}" ]; then
printf '\n'
break
fi
done
) | grep ^ > /dev/null && return
done
}
Example:
$ ask 'continue?' 'y=yes|n=no|m=maybe'
continue? [y=yes|n=no|m=maybe] g
continue? [y=yes|n=no|m=maybe] k
continue? [y=yes|n=no|m=maybe] y
$
It will set REPLY
to y
(inside the script).

- 7,526
- 8
- 34
- 40
Inspired by the answers of @Mark and @Myrddin I created this function for a universal prompt
uniprompt(){
while true; do
echo -e "$1\c"
read opt
array=($2)
case "${array[@]}" in *"$opt"*) eval "$3=$opt";return 0;; esac
echo -e "$opt is not a correct value\n"
done
}
use it like this:
unipromtp "Select an option: (a)-Do one (x)->Do two (f)->Do three : " "a x f" selection
echo "$selection"

- 395
- 1
- 5
- 13
I suggest you use dialog...
Linux Apprentice: Improve Bash Shell Scripts Using Dialog
The dialog command enables the use of window boxes in shell scripts to make their use more interactive.
it's simple and easy to use, there's also a gnome version called gdialog that takes the exact same parameters, but shows it GUI style on X.

- 1
- 1

- 5,654
- 5
- 28
- 48
-
For the casual reader, have look to a snippet using the dialog command here: http://stackoverflow.com/a/22893526/363573 – Stephan Dec 02 '14 at 19:41
more generic would be:
function menu(){
title="Question time"
prompt="Select:"
options=("Yes" "No" "Maybe")
echo "$title"
PS3="$prompt"
select opt in "${options[@]}" "Quit/Cancel"; do
case "$REPLY" in
1 ) echo "You picked $opt which is option $REPLY";;
2 ) echo "You picked $opt which is option $REPLY";;
3 ) echo "You picked $opt which is option $REPLY";;
$(( ${#options[@]}+1 )) ) clear; echo "Goodbye!"; exit;;
*) echo "Invalid option. Try another one.";continue;;
esac
done
return
}

- 87
- 7
The absolute most simple solution is this one-liner without clever tricks:
read -p "press enter ..." y
It reminds of the classic DOS Hit any key to continue
, except that it waits for the Enter key, not just any key.
True, this does not offer you three options for Yes No Cancel, but it is useful where you accept control-C as No resp. Cancel in simple scripts like, e.g.:
#!/bin/sh
echo Backup this project
read -p "press enter ..." y
rsync -tavz . /media/hard_to_remember_path/backup/projects/yourproject/
because you don't like to need to remember ugly commands and paths, but neither scripts that run too fast, without giving you a chance to stop before you decide it is not the script you intended to run.
The command line argument y
is required with sh
and can optionally be used to receive the answer typed by the user before pressing the Enter key, like this:
echo You entered $y
With bash
you may omit the last argument and just use:
read -p "press enter ..."

- 4,619
- 7
- 49
- 81
yn() {
if [[ 'y' == `read -s -n 1 -p "[y/n]: " Y; echo $Y` ]];
then eval $1;
else eval $2;
fi }
yn 'echo yes' 'echo no'
yn 'echo absent no function works too!'

- 1,113
- 7
- 13
-
This seems complex and fragile. How about just `yn(){ read -s -n 1 -p '[y/n]'; test "$REPLY" = "y" ; } yn && echo success || echo failure` – tripleee May 21 '14 at 11:44
One simple way to do this is with xargs -p
or gnu parallel --interactive
.
I like the behavior of xargs a little better for this because it executes each command immediately after the prompt like other interactive unix commands, rather than collecting the yesses to run at the end. (You can Ctrl-C after you get through the ones you wanted.)
e.g.,
echo *.xml | xargs -p -n 1 -J {} mv {} backup/

- 5,059
- 2
- 34
- 39
-
Not bad, but `xargs --interactive` is limited to yes or no. As long as that's all you need that can be enough, but my original question gave an example with three possible results. I really like that it is streamable though; many common scenarios would benefit from its ability to be piped. – Myrddin Emrys Apr 09 '15 at 00:25
-
I see. My thinking was that "cancel" meant to simply stop all further execution, which this supports via Ctrl-C, but if you need to do something more complex on cancel (or on no) this won't suffice. – Joshua Goldberg Jun 07 '15 at 22:21
As a friend of a one line command I used the following:
while [ -z $prompt ]; do read -p "Continue (y/n)?" choice;case "$choice" in y|Y ) prompt=true; break;; n|N ) exit 0;; esac; done; prompt=;
Written longform, it works like this:
while [ -z $prompt ];
do read -p "Continue (y/n)?" choice;
case "$choice" in
y|Y ) prompt=true; break;;
n|N ) exit 0;;
esac;
done;
prompt=;

- 42,126
- 11
- 38
- 51

- 633
- 2
- 6
- 17
-
Can you clarify the use of the prompt variable? Looks to me like it's wiped after the one liner, so how do you use the line to DO anything? – Myrddin Emrys Jun 05 '15 at 06:37
-
prompt is wiped after the while loop. Because I want the prompt variable to be initialized afterwards (since I am using the statement more often). Having this line in a shell-script will only proceed if y|Y is typed and exit if n|N is typed or repeat asking for input for everything else. – ccDict Jun 05 '15 at 06:42
I've used the case
statement a couple of times in such a scenario, using the case statment is a good way to go about it. A while
loop, that ecapsulates the case
block, that utilizes a boolean condition can be implemented in order to hold even more control of the program, and fulfill many other requirements. After the all the conditions have been met, a break
can be used which will pass control back to the main part of the program. Also, to meet other conditions, of course conditional statements can be added to accompany the control structures: case
statement and possible while
loop.
Example of using a case
statement to fulfill your request
#! /bin/sh
# For potential users of BSD, or other systems who do not
# have a bash binary located in /bin the script will be directed to
# a bourne-shell, e.g. /bin/sh
# NOTE: It would seem best for handling user entry errors or
# exceptions, to put the decision required by the input
# of the prompt in a case statement (case control structure),
echo Would you like us to perform the option: "(Y|N)"
read inPut
case $inPut in
# echoing a command encapsulated by
# backticks (``) executes the command
"Y") echo `Do something crazy`
;;
# depending on the scenario, execute the other option
# or leave as default
"N") echo `execute another option`
;;
esac
exit

- 193
- 1
- 11
Yes / No / Cancel
Function
#!/usr/bin/env bash
@confirm() {
local message="$*"
local result=''
echo -n "> $message (Yes/No/Cancel) " >&2
while [ -z "$result" ] ; do
read -s -n 1 choice
case "$choice" in
y|Y ) result='Y' ;;
n|N ) result='N' ;;
c|C ) result='C' ;;
esac
done
echo $result
}
Usage
case $(@confirm 'Confirm?') in
Y ) echo "Yes" ;;
N ) echo "No" ;;
C ) echo "Cancel" ;;
esac
Confirm with clean user input
Function
#!/usr/bin/env bash
@confirm() {
local message="$*"
local result=3
echo -n "> $message (y/n) " >&2
while [[ $result -gt 1 ]] ; do
read -s -n 1 choice
case "$choice" in
y|Y ) result=0 ;;
n|N ) result=1 ;;
esac
done
return $result
}
Usage
if @confirm 'Confirm?' ; then
echo "Yes"
else
echo "No"
fi

- 17,828
- 6
- 117
- 94
This is what I usually need in a script/function:
- default answer is Yes, if you hit ENTER
- accept also z (in case you mix up you are on QWERTZ Layout)
- accept other lanyuages ("ja", "Oui", ...)
- handle the right exit in case you are inside a function
while true; do
read -p "Continue [Y/n]? " -n 1 -r -e yn
case "${yn:-Y}" in
[YyZzOoJj]* ) echo; break ;;
[Nn]* ) [[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1 ;; # handle exits from shell or function but don't exit interactive shell
* ) echo "Please answer yes or no.";;
esac
done
echo "and off we go!"

- 19,527
- 31
- 134
- 226
In response to others:
You don't need to specify case in BASH4 just use the ',,' to make a var lowercase. Also I strongly dislike putting code inside of the read block, get the result and deal with it outside of the read block IMO. Also include a 'q' for quit IMO. Lastly why type 'yes' just use -n1 and have the press y.
Example: user can press y/n and also q to just quit.
ans=''
while true; do
read -p "So is MikeQ the greatest or what (y/n/q) ?" -n1 ans
case ${ans,,} in
y|n|q) break;;
*) echo "Answer y for yes / n for no or q for quit.";;
esac
done
echo -e "\nAnswer = $ans"
if [[ "${ans,,}" == "q" ]] ; then
echo "OK Quitting, we will assume that he is"
exit 0
fi
if [[ "${ans,,}" == "y" ]] ; then
echo "MikeQ is the greatest!!"
else
echo "No? MikeQ is not the greatest?"
fi

- 6,716
- 5
- 55
- 62
I've made this small script for yes/no questions: https://github.com/optimistiCli/getans
Example:
#!/bin/bash
if ! getans.sh 'Shall we proceed?' y ; then
echo "User said “NO”"
exit 1
fi
echo "User said “YES”"
# do something usefull
exit 0
Direct link: https://github.com/optimistiCli/getans/raw/main/getans.sh

- 81
- 2
A one-liner python
alternative using PyInquirer
python3 -c 'import PyInquirer; print(PyInquirer.prompt([{"type":"confirm", "message":"Do you want to continue?", "name":"r"}]).get("r"))'
which supports yes/no/cancel (intr, CTRL+C).

- 65,697
- 9
- 111
- 134
You can write a function to test:
confirm() {
local ans IFS=;
while read -rp "$1" -n1 ans;
do printf '\n';
case $ans in [Yy]) return 0;;
[Nn]) return 1;;
esac;
done;
}; ## Usage: if confirm "Are you sure? "; then ...
if confirm "Does everything look ok...reboot now? [Y/n]"; then
echo "rebooting..."
sleep 5
reboot
fi

- 72,281
- 52
- 227
- 295
I created this function to be called by all my bash scripts that require a yes/no/true/false/default response, as I was tired of recreating the code every time I needed a response.
It has a settable question, and settable default answer with error handling for the default response (meaning you can change what the default answer is), as well as capturing questions that fail to be surrounded by quotes. I realize this is more detailed than the original post asked for, but this should be fully portable in any bash 4+. It is usable as a command in if statements as well, as it returns 0 or 1, as well as detailing back what the answer was for debugging.
Usage: confirm -d n "Is this the answer?"
Which returns the output:
user@ubuntu# confirm -d n "Is this the answer?"
Is this the answer? [Y/n:default=N]:
default no
Here's the function:
confirm(){
errorMsg='
Too many arguments.
use "-d" as the first option to set default value.
If providing a question, remember to quote the line to prevent each word from being an argument.
example: confirm -d n "Is this the answer?"'
defSet=false
while [ "$#" -ge 1 ]; do
if [ "$#" -ge 2 ]; then
confirmArg=$1
if [[ "$confirmArg" = '-d' ]]; then
confirmArgCheck=$2
errorMsg2="
'${confirmArgCheck}' is not a valid default return value.
Must be [Yy]es/[Nn]o or [Tt]rue/[Ff]alse"
case $confirmArgCheck in
[Yy]*|[Tt]*)
regex1="[Yy][Ee]?[Ss]?$"
regex2="[Tt][Rr]?[Uu]?[Ee]?$"
if [[ "$confirmArgCheck" =~ $regex1 ]]; then
unset regex1 && unset regex2
confirmArgVal=Y
elif [[ "$confirmArgCheck" =~ $regex2 ]]; then
unset regex1 && unset regex2
confirmArgVal=Y
else
unset regex1 && unset regex2
echo "$errorMsg2"
return 1
fi
;;
[Nn]*|[Ff]*)
regex1="[Nn][Oo]?$"
regex2="[Ff][Aa]?[Ll]?[Ss]?[Ee]?$"
if [[ "$confirmArgCheck" =~ $regex1 ]]; then
unset regex1 && unset regex2
confirmArgVal=N
elif [[ "$confirmArgCheck" =~ $regex2 ]]; then
unset regex1 && unset regex2
confirmArgVal=N
else
unset regex1 && unset regex2
echo "$errorMsg2"
return 1
fi
;;
*)
echo "$errorMsg2"
return 1
;;
esac
defSet=true
shift 2
else
if [[ ! "$*" == *"-d"* ]]; then
shiftmore="$#"
quest="$@"
questSet=true
shift "$shiftmore"
else
echo "$errorMsg"
return 1
fi
fi
else
quest=$1
questSet=true
shift 1
fi
done
if [[ ! $defSet = true ]]; then
confirmArgVal=Y
fi
if [[ ! $questSet = true ]]; then
quest='Answer?'
fi
while true; do
read -r -p "${quest} [Y/n:default=$confirmArgVal]: " yn
case $yn in
[Yy]*)
unset quest
unset yn
unset confirmArgVal
echo "answered yes"
return 0
;;
[Nn]*)
unset quest
unset yn
unset confirmArgVal
echo "answered no"
return 1
;;
"")
unset quest
unset yn
if [[ "$confirmArgVal" = 'Y' ]]; then
unset confirmArgVal
echo "default yes"
return 0
else
unset confirmArgVal
echo "default no"
return 1
fi
;;
*)
echo "Please answer yes or no."
;;
esac
done
}

- 33
- 6
Here's yet another approach with a function that returns 0 for success, 1 for failure. There's no re-prompt; y
/Y
confirms and anything else aborts.
#!/usr/bin/env bash
set -eu -o pipefail
function prompt() {
read -p "$* [Y/n]: " yn
if [[ $yn = "y" || $yn = "Y" ]]; then
return 0
else
return 1
fi
}
prompt "one liner?" && echo "YES" || echo "NO"
if prompt "in an if?"; then
echo "YES"
else
echo "NO"
fi

- 44,755
- 7
- 76
- 106