What is the point of the "yes" command?

yes can be used to send an affirmative (or negative; e.g. yes n) response to any command that would otherwise request one, thereby causing the command to run non-interactively.

The yes command in conjunction with the head command can be used to generate large volume files for means of testing.

It can also be used to test how well a system handles high loads, as using yes results in 100% processor usage, for systems with a single processor (for a multiprocessor system, a process must be run for each processor). This, for example, can be useful for investigating whether a system's cooling system will be effective when the processor is running at 100%.

In 2006, the yes command received publicity for being a means to test whether or not a user's MacBook is affected by the Intermittent Shutdown Syndrome. By running the yes command twice via Terminal under Mac OS X, users were able to max out their computer's CPU, and thus see if the failure was heat related

via wikipedia: http://en.wikipedia.org/wiki/Yes_(Unix)


This might be a controversial opinion, but in my view it is an ugly fix for bad user interface in command-line tools.

Some command-line tools ask questions to the user with a prompt and do not have an option to run non-interactively; imagine for instance something such as

$ frobnicate *
frobnicate file a.txt? (y/n) y
frobnicate file b.txt? (y/n) y
...

Since the answer to the question is taken from standard input, a quick fix to this problem is having an application that outputs the string y\n continuously, which is exactly what yes does. Unix pipes can be used to send this output as an input to a given command.

$ yes | frobnicate

One of the issues with this approach is that yes has no possibility to check the question it is answering to:

frobnicate file a.txt? (y/n) y
frobnicate file b.txt? (y/n) y
format device /dev/sda1? (y/n) y
frobnicate file c.txt? (y/n) y

A better solution, when it is available, is a specific option to run non-interactively, such as rm -f or apt-get -y. This allows coding more sensible behavior in the application.


I like to use yes when I am unzipping multiple .zip files that all contain the same file with the same name and I am asked what to do in each case (ex. a licence agreement).

yes | for z in *.zip; do unzip "$z"; done 

Tags:

Command

Yes