Is it possible to have a bash script automatically handle prompts that would normally be presented to the user with default actions? Currently I am using a bash script to call an in-house tool that will display prompts to the user (prompting for Y/N) to complete actions, however the script I'm writing needs to be completely "hands-off", so I need a way to send Y|N
to the prompt to allow the program to continue execution. Is this possible?

- 1,029
- 15
- 22

- 23,914
- 7
- 59
- 77
-
this is not "autocompletion"... like @unwind says, it's "automation" – benzaita Jun 26 '12 at 12:55
-
3Example of using `yes` to do this: http://unix.stackexchange.com/questions/47344/the-yes-command#comment65866_47344 – yurisich Sep 22 '15 at 15:01
-
use printf and pipe to script. printf "yes\nNO\nYes" | ./questions.sh – Linden X. Quan Sep 24 '21 at 17:53
6 Answers
A simple
echo "Y Y N N Y N Y Y N" | ./your_script
This allow you to pass any sequence of "Y" or "N" to your script.

- 7,540
- 8
- 39
- 51
-
3If I needed to send any `N`'s, I would have used this method, but I only needed Y's so I went with `yes`. Too bad I can't accept two answers, since they are both correct. – TJ L Sep 27 '10 at 14:43
-
4;) Well if you ever need to send some `N` you now have a good way to do it. – Loïc Février Sep 27 '10 at 14:46
-
2This doesn't work for me; spaces do not count as new lines---if for example I echo "Y N" then it gives the string `"Y N"` as input to the first prompt and hangs on the second prompt waiting for me to complete it. I tried changing the spaces to newlines but that didn't work either, still gave a literal string to the prompt. – temporary_user_name May 27 '15 at 22:45
-
65try using `printf 'y\ny\ny\n' | ./your_script`. This way you are manually inserting the newline for each expected entry. – Majid Jun 22 '15 at 02:26
-
1Is this possible to do for making ordinary calls, let's say 'sudo git pull' for example? @LoïcFévrier – danielo Feb 06 '18 at 23:35
-
19`printf '%s\n' Y Y N N Y N Y Y N` puts newlines between the items, but without needing one big format string. – Charles Duffy Jun 22 '18 at 15:14
-
2@danielo, `sudo` and `ssh` aren't "ordinary" -- they prompt from the TTY, not stdin, for security reasons. That said, you **really** shouldn't be using sudo to run git. Better to fix your permissions so your regular user account owns all the files involved; doing version control operations with elevated privileges means that a security bug in git 0wns your whole box, not just your personal account.. – Charles Duffy Jun 22 '18 at 15:14
-
`echo [any answer] | [comand]` is the cool. replace [any answer] with any reply Y,N,1,2,C, etc and [comand] with your command/script. – Dexter Jul 23 '23 at 01:48
-
3Unfortunately I can't use expect as their are stringent space requirements on the system running this so I can't add extra packages, but piping `yes` in did the trick, luckily all the prompts only required a 'y' anyway. Thanks. – TJ L Sep 27 '10 at 14:32
-
However `Yes | command` may not work always. If it is just for installation purpose then I fell using `-y` is a better and preferred option. – Umesh Kaushik Mar 14 '17 at 10:13
-
2Can `Expect` answer password prompts? For example, `git pull` requires me to enter my username and password. The password doesn't show on screen, it is hidden. Can `Expect` manage that? – Xbox One Nov 11 '20 at 17:43
-
@XboxOne: I'm virtually certain the answer is "YES" (as far as I recall I think I've done that with `expect`) but if you search for that question on StackOverflow or Unix.SE I'm sure it has been asked already. And I'm pretty sure you can find examples of it quite easily. Asking questions in comments doesn't usually yield good answers. – iconoclast Mar 29 '22 at 20:38
If you only have Y to send :
$> yes Y |./your_script
If you only have N to send :
$> yes N |./your_script

- 2,274
- 1
- 18
- 24

- 697
- 6
- 6
I found the best way to send input is to use cat and a text file to pass along whatever input you need.
cat "input.txt" | ./Script.sh

- 239
- 2
- 2
-
2Expect did not work as expected for me. This is the simplest and best answer to this question. – cloudxix May 16 '20 at 21:02
-
The [`cat` is useless](https://stackoverflow.com/questions/11710552/useless-use-of-cat) here; you want `./Script.sh
– tripleee Nov 02 '21 at 10:59 -
1@tripleee: the `cat` is only useless if you don't care about readability and maintaining the logical flow from left-to-right. Whether the other option is more performant is another matter, but strictly speaking it's not true to say that the `cat` gives no value. That's why so many people still use this despite all the complaints. – iconoclast Mar 29 '22 at 20:41
In my situation I needed to answer some questions without Y or N but with text or blank. I found the best way to do this in my situation was to create a shellscript file. In my case I called it autocomplete.sh
I was needing to answer some questions for a doctrine schema exporter so my file looked like this.
-- This is an example only --
php vendor/bin/mysql-workbench-schema-export mysqlworkbenchfile.mwb ./doctrine << EOF
`#Export to Doctrine Annotation Format` 1
`#Would you like to change the setup configuration before exporting` y
`#Log to console` y
`#Log file` testing.log
`#Filename [%entity%.%extension%]`
`#Indentation [4]`
`#Use tabs [no]`
`#Eol delimeter (win, unix) [win]`
`#Backup existing file [yes]`
`#Add generator info as comment [yes]`
`#Skip plural name checking [no]`
`#Use logged storage [no]`
`#Sort tables and views [yes]`
`#Export only table categorized []`
`#Enhance many to many detection [yes]`
`#Skip many to many tables [yes]`
`#Bundle namespace []`
`#Entity namespace []`
`#Repository namespace []`
`#Use automatic repository [yes]`
`#Skip column with relation [no]`
`#Related var name format [%name%%related%]`
`#Nullable attribute (auto, always) [auto]`
`#Generated value strategy (auto, identity, sequence, table, none) [auto]`
`#Default cascade (persist, remove, detach, merge, all, refresh, ) [no]`
`#Use annotation prefix [ORM\]`
`#Skip getter and setter [no]`
`#Generate entity serialization [yes]`
`#Generate extendable entity [no]` y
`#Quote identifier strategy (auto, always, none) [auto]`
`#Extends class []`
`#Property typehint [no]`
EOF
The thing I like about this strategy is you can comment what your answers are and using EOF a blank line is just that (the default answer). Turns out by the way this exporter tool has its own JSON counterpart for answering these questions, but I figured that out after I did this =).
to run the script simply be in the directory you want and run 'sh autocomplete.sh'
in terminal.
In short by using << EOL & EOF in combination with Return Lines you can answer each question of the prompt as necessary. Each new line is a new answer.
My example just shows how this can be done with comments also using the ` character so you remember what each step is.
Note the other advantage of this method is you can answer with more then just Y or N ... in fact you can answer with blanks!
Hope this helps someone out.

- 8,659
- 12
- 83
- 154
There is a special build-in util for this - 'yes'.
To answer all questions with the same answer, you can run
yes [answer] |./your_script
Or you can put it inside your script have specific answer to each question

- 15,177
- 12
- 106
- 130
-
2how can i do that inside the script? i mean to specify different answers for each question – Sengo Jun 24 '20 at 13:40
-
-
`yes $'hello\ngoodbye\nso long'` iterates over those three, forever. (The `$'...'` "C string" notation is specific to Bash; but you can use regular single quotes and literal newlines in any Bourne-compatible shell.) – tripleee Nov 02 '21 at 11:00
-
`yes $(date|md5sum) | security add-generic-password -s facebook -a xx.yy -w` adds a password to Keychain without prompt to input password. – DawnSong May 23 '22 at 01:39