How do I always answer No to any prompt with a bash script?

Viewed 23503

I have a program that runs and asks users certain questions. I want to automate it so that every question is responded to with No.

5 Answers
yes no | <command>

Where <command> is the command you want to answer no to.

(or yes n if you actually need to just output an n)

The yes command, by default, outputs a continuous stream of y, in order to answer yes to every prompt. But you can pass in any other string as the argument, in order for it to repeat that to every prompt.

As pointed out by "just somebody", yes isn't actually standardized. While it's available on every system I've ever used (various BSDs, Mac OS X, Linux, Solaris, Cygwin), if you somehow manage to find one in which it doesn't, the following should work:

while true; do echo no; done | <command>

Or as a full-fledged shell script implementation of yes, you can use the following:

#!/bin/sh

if [ $# -ge 1 ]
then
    while true; do echo "$1"; done
else
    while true; do echo y; done
fi

actually, it looks funny ...

$ yes no

manpages excerpt:

$ man yes 

YES(1)                    BSD General Commands Manual                   YES(1)

NAME
     yes -- be repetitively affirmative

SYNOPSIS
     yes [expletive]

DESCRIPTION
     yes outputs expletive, or, by default, ``y'', forever.

...

for systems with no such command, just a simple echo should work

echo "no" | command

for repetitions , not that hard to make a while/for loop that goes on forever.

just in case you might be interested in some portability: yes(1) is nonstandard in that it's not described in the Single Unix Specification (POSIX by another name). but it's quite portable anyway (see the HISTORY paragraph; pity The MYYN didn't quote the whole thing):

YES(1)                  FreeBSD General Commands Manual                 YES(1)

NAME
     yes — be repetitively affirmative

SYNOPSIS
     yes [expletive]

DESCRIPTION
     The yes utility outputs expletive, or, by default, “y”, forever.

HISTORY
     The yes command appeared in Version 32V AT&T UNIX.

FreeBSD 9.0                      June 6, 1993                      FreeBSD 9.0

edit

in case you hit an odd system that does not implement this command, it's trivial to provide it yourself. this from FreeBSD-9:

int
main(int argc, char **argv)
{
 if (argc > 1)
  while (puts(argv[1]) != EOF)
   ;
 else
  while (puts("y") != EOF)
   ;
 err(1, "stdout");
 /*NOTREACHED*/
}

If you find that yes answers too fast for some reason, you can slow things down a little by using a modified version of @Brian Campbell 's answer - this adds in 0.1 seconds of delay in between each repitition of the word "no"

while true; do echo no; sleep 0.1s; done | <command here> 
Related