131

I work with Amazon Linux instances and I have a couple scripts to populate data and install all the programs I work with, but a couple of the programs ask:

Do you want to continue [Y/n]?

and pause the install. I want to auto answer "Y" in all cases, I'm just now sure how to do it.

user123456
  • 364
  • 3
  • 15
user974887
  • 1,739
  • 2
  • 14
  • 15

6 Answers6

236

The 'yes' command will echo 'y' (or whatever you ask it to) indefinitely. Use it as:

yes | command-that-asks-for-input

or, if a capital 'Y' is required:

yes Y | command-that-asks-for-input

If you want to pass 'N' you can still use yes:

yes N | command-that-asks-for-input
Vishrant
  • 10,695
  • 8
  • 48
  • 87
P.T.
  • 23,368
  • 7
  • 60
  • 92
  • does this only work with some command supported to use input from yes, doesn't it? I tried with glance from OpenStack and this not work, I think Expect is more precise for all circumstances – HVNSweeting Nov 01 '12 at 08:53
  • 1
    // , What if you have to enter the full word "yes"? – Nathan Basanese Dec 09 '15 at 10:16
  • 1
    Be careful with `yes` as it is known to max out the CPU. http://stackoverflow.com/a/18164007/720665 – David Salamon Aug 02 '16 at 09:44
  • Nice. This just made my life substantially easier. :D. Also googling `-y` for bash scripts was HARD. – Nathan Smith Mar 29 '17 at 10:30
  • 2
    @DavidSalamon that's only the case when it's writing to something without limit, like `/dev/null` or STDOUT. Piped to a command, it will only write one line to the pipe each time the receiving command reads one, and will wait otherwise. – Walf Feb 07 '19 at 04:52
  • Bevare that `yes` command may fail script if `set -eo pipefail` is enabled. It may be safer/more portable to use `echo y` – Alexander Pogrebnyak Feb 24 '20 at 22:08
  • If you need a password enter, sudo -S: `echo "notsecure" | sudo -S apt-get update` – Timo Nov 07 '20 at 17:43
  • Is the enter included in yes? – Timo Nov 07 '20 at 17:46
75

echo y | command should work.

Also, some installers have an "auto-yes" flag. It's -y for apt-get on Ubuntu.

Dennis
  • 13,205
  • 2
  • 43
  • 57
  • I am trying to to do echo "giturl no" | jspm registry create registry_name jspm-git in my shell script. but it keeps on failing. while entering the giurl. Any idea? – manismku Oct 20 '17 at 03:49
  • Another example: In docker-compose there is `-f` (`--force`) flag which works as "auto-yes" for commands that remove something – luke Dec 19 '17 at 07:47
  • How do you "echo Y" recursively in case the prompt asks for a confirmation more than once? – Safak Ozkan Feb 21 '18 at 01:46
  • This is also useful for pip. ex: `echo y | pip uninstall psycopg2-binary` – Tony Chou Oct 21 '20 at 07:02
18

You might not have the ability to install Expect on the target server. This is often the case when one writes, say, a Jenkins job.

If so, I would consider something like the answer to the following on askubuntu.com:

https://askubuntu.com/questions/338857/automatically-enter-input-in-command-line

printf 'y\nyes\nno\nmaybe\n' | ./script_that_needs_user_input

Note that in some rare cases the command does not require the user to press enter after the character. in that case leave the newlines out:

printf 'yyy' | ./script_that_needs_user_input

For sake of completeness you can also use a here document:

./script_that_needs_user_input << EOF
y
y
y
EOF

Or if your shell supports it a here string:

./script <<< "y
y
y
"

Or you can create a file with one input per line:

./script < inputfile

Again, all credit for this answer goes to the author of the answer on askubuntu.com, lesmana.

Luc
  • 2,909
  • 2
  • 37
  • 38
Nathan Basanese
  • 7,464
  • 8
  • 32
  • 61
7

You just need to put -y with the install command.

For example: yum install <package_to_install> -y

Rohan Seth
  • 1,632
  • 10
  • 8
6

Although this may be more complicated/heavier-weight than you want, one very flexible way to do it is using something like Expect (or one of the derivatives in another programming language).

Expect is a language designed specifically to control text-based applications, which is exactly what you are looking to do. If you end up needing to do something more complicated (like with logic to actually decide what to do/answer next), Expect is the way to go.

Adam Batkin
  • 47,187
  • 7
  • 120
  • 110
3

If you want to just accept defaults you can use:

\n | ./shell_being_run
warhansen
  • 638
  • 1
  • 6
  • 19