The yes Command

The yes command is one of the simplest commands in Linux and other Unix-like operating systems like macOS. And by simple, we mean simple in its use and its initial implementation. The source code for the original version—released in System 7 Unix and authored by Ken Thompson—amounts to a mere six lines of code.

But don’t write it off for being a simple little command. It can be used in some interesting and useful ways.

What Does yes Do?

Used without any command line parameters, the yes command behaves as though you were typing “y” and hitting Enter, over and over (and over and over) again. Very quickly. And it will carry on doing so until you press Ctrl+C to interrupt it.

In fact, yes can be used to repeatedly generate any message you choose. Simply type yes, a space, the string you wish to use, and then press Enter. This is often used to cause yes to generate an output stream of “yes” or “no” strings.

But What Use Is That?

The output from yes can be piped into other programs or scripts.

Does this sound familiar? You start a long process running and step away, leaving it to run. When you return to your computer, the process hasn’t completed at all. In your absence, it has asked you a question and is sat waiting for a “yes” or “no” response.

If you know in advance that all your answers are going to be positive (“yes” or “y”) or negative (“no” or “n”) you can use yes to provide those responses for you. Your long process will then run through to completion unattended with yes providing the answers to any questions the process asks.

Using yes With Scripts

Look at the following Bash shell script. (We need to imagine that this is a part of a much larger script that will take a considerable time to run.)

This script asks a question and awaits a response. The logic flow within the script is decided upon by the input from the user.

A “yes” or “y” indicates a positive response. Any other input is considered a negative response. Pressing Enter with no input text does nothing.

To test this, copy the script to a file and save it as long_script.sh. Use chmod to make it executable.

Run the script with the following command. Try providing “yes,” “y,” and anything else as input, including pressing Enter with no input text.

To get yes to provide our response to the script’s question, pipe the output from yes to the script.

Some scripts are more rigid in their requirements and only accept the full word “yes” as a positive response. You can provide “yes” as a parameter to yes, as follows:

Don’t Say yes Without Thinking It Through

You need to be certain that the input you are going to feed into the script or program is definitely going to give you the outcome you expect. To be able to make that decision, you must know the questions and what your responses should be.

The logic in the script, command, or program might not match your expectations. In our example script, the question might have been “Do you wish to stop? [y,n].” If that had been the case, a negative response would have allowed the script to proceed.

You must be familiar with the script, command, or program before you blithely pipe yes into it.

Using yes With Commands

In its infancy, yes would be used with other Linux commands. Since then, most of those other Linux commands have their own way of running without human interaction. yes is no longer required to achieve that.

Let’s take the Ubuntu package manager apt-get as an example. To install an application without having to press “y” half-way through the installation, yes would have been used as follows:

The same result can be achieved using the -y (assume yes) option in apt-get:

You’ll see that apt-get didn’t even ask its usual “Do you want to continue? [Y/n]” question. It just assumed the answer would be “yes.”

On other Linux distributions, the situation is the same. On Fedora you would have used this type of  package manager command at one time:

The dnf package manager has replaced yum and dnf has its own -y (assume yes) option.

The same applies to cp, fsck, and rm. These commands each have their own -f (force) or -y (assume yes) options.

So does it seem that yes has been relegated to only working with scripts?  Not quite. There are a few more tricks in the old dog yet.

Some Further yes Tricks

You can use yes with a sequence of digits generated by seq to control a loop of repeated actions.

This one-liner echoes the generated digits to the terminal window and then calls sleep for one second.

Instead of simply echoing the digits to the terminal window, you could call another command or script. That command or script doesn’t even need to use the digits, and they’re only there to kick-start each cycle of the loop.

Sometimes it is useful to have a large file to test with. Perhaps you want to practice using the zip command, or you want to have a sizeable file to test FTP uploads with.

You can rapidly generate large files with yes. All you need to do is give it a long string of text to work with and redirect the output into a file. Make no mistake; those files will grow rapidly. Be ready to press Ctrl+C within a few seconds.

The file generated here took about five seconds on the test machine used to research this article.  ls reports that it is 557 Mb in size, and wc tell us there are 12.4 million lines in it.

We can limit the size of the file by including head in our command string. We tell it how many lines to include in the file. The -50 means head will let just 50 lines through to the test.txt file.

As soon as there are 50 lines in the test.txt file, the process will stop. You don’t need to use Ctrl+C. It comes to a graceful halt on its own volition.

wc reports that there are exactly 50 lines in the file, 400 words and it is 2350 bytes in size.

Even though it is still useful for feeding responses into long-running scripts (and a few other tricks), the yes command isn’t going to be a part of your daily toolkit of commands. But when you do need it, you’ll find it is simplicity itself—and all in six lines of golden code.