How to use the yes command in Linux

Contents

The yes command seems too simple to be of practical use, but in this tutorial, We'll show you your app and how to benefit from its cumulative positivity on Linux and macOS.

The command yes

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

But don't rule it out for being a simple command. Can be used in interesting and useful ways.

What does the yes?

Used without any command line parameters, the yes The command behaves as if you were typing "y" and pressing Enter, and again (and again and again). Very quickly. And it will continue to do so until you press Ctrl + C to interrupt it.

yes

Actually, yes can be used to repeatedly generate any message you choose. Just type yes, a space, the string you want to use and then hit Enter. This is often used to cause yes to generate a string output stream “Yes” O “no”.

yes yes

yes anything you like

But, What's the use of that?

The output of yes can be piped to other programs or scripts.

Does it sound familiar to you? You start a long running procedure and walk away, letting it work. When you return to your computer, the procedure is not completed at all. In his absence, you have asked him a question and are sitting waiting for an answer from “Yes” O “no”.

If you know in advance that all your answers will be positive (“Yes” O “and”) the negative (“no” O “n”), You can use yes to provide you with those answers. Your lengthy procedure will run to completion unsupervised with yes providing answers to procedural questions.

Use yes with scripts

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

#!/bin/bash

# ...
# in the middle of some long script
# obtain a response from the user
# ...

echo "Are you happy to proceed? [and,n]"
read input

# did we get an input value?
if [ "$input" == "" ]; then

   echo "Nothing was entered by the user"

# was it a y or a yes?
elif [[ "$input" == "and" ]] || [[ "$input" == "yes" ]]; then

   echo "Positive response: $input"

# treat anything else as a negative response
else

   echo "negative response: $input"

be

This script asks a question and expects an answer. The logical flow within the script is decided by user input.

  • a “Yes” O “and” indicates a positive response.
  • Any other entry is considered a negative response.
  • Pressing Enter without entering 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.

chmod +x long_script.sh

Run the script with the following command. Try to provide “Yes”, “and” and anything else as input, including pressing Enter without entering text.

./long_script.sh

To get yes to provide our answer to the script question, pipe the output of yes to the script.

yes | ./long_script.sh

Some scripts are more rigid in their requirements and only accept the whole word "yes" as a positive answer.. Can provide “Yes” as a parameter for yes, as follows:

yes yes | ./long_script.sh

Don't say yes without thinking about it

You must be sure that the input you are going to enter in the script or program will definitely give you the result you expect.. To be able to make that decision, you should know the questions and what your answers should be.

It is feasible that the script logic, command or program does not match your expectations. In our example script, the question might have been “Do you want to stop? [and,n]. ” If that had been the case, a negative answer would have allowed the script to continue.

you has to be familiar with the script, command or program before happily channeling yes On it.

Use yes with commands

In his infancy, yes would be used with other Linux commands. Since then, most of those other Linux commands have their own way to run without human interaction. yes it is no longer necessary to achieve it.

Let's take the Ubuntu package manager apt-get as an example. To install an app without having to press “and” halfway through the installation, yes would have been used in the following way:

yes | sudo apt-get install fortune-mod

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

sudo apt-get -y install fortune-mod

You will see that apt-get he didn't even ask his usual "Do you wish to continue? [Y / n]” question. He just assumed the solution would be “Yes”.

On other Linux distributions, the situation is the same. In Fedora, I would have used this kind of package manager command at the same time:

yes | yum install fortune-mod

the dnf package manager has replaced yum and dnf It has its own -y (suppose yes) option.

dnf -y install fortune-mod

The same applies to cp, fsck, and rm. Each of these commands has its own -f (strength) O -y (suppose yes) options.

So it seems that yes has been relegated to working with scripts only? Not quite. There are still a few more tricks on the old dog.

Some more tricks yes

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

This single line echoes the generated digits in the terminal window and then calls sleep for one second.

Instead of just sending the digits to the terminal window, you can call another command or script. That command or script doesn't even need to use the digits, and they're only there to start each cycle of the cycle.

yes "$(seq 1 20)" | while read digit; do echo digit; sleep 1; done

yes one-liner that regulates a loop in the terminal window

Sometimes it helps to have a large file to test. You may want to practice using the zip command, or you want to have a sizeable archive to test your FTP uploads.

You can quickly generate large files with yes. All you need to do is give it a long text string to work with and redirect the output to a file. Not make mistakes; those files will grow quickly. Be prepared to press Ctrl + C in a few seconds.

yes long line of meaningless text for file padding > test.txt
ls -lh test.txt
wc test.txt

generating test files with a terminal window yes ia

The file generated here took about five seconds on the test machine used to investigate this post. ls reports that it has a size of 557 Mb, and wc tell us what it contains 12,4 million lines.

We can limit the file size by including head in our command string. We tell you how many lines to include in the file. the -50 half head will leave alone 50 lines across the test.txt proceedings.

yes long line of meaningless text for file padding | head -50 > test.txt

using the head to limit the size of a file in a terminal window

As soon as there is 50 lines in the test.txt file, the procedure will stop. No need to use Ctrl + C. Stops gracefully of its own accord.

wc informs that there is exactly 50 lines in file, 400 words and has a size of 2350 bytes.


Still useful for inputting responses in long-running scripts (and some other tricks), the yes command will not be part of your daily command toolkit. But when you need it, you will find that it is simplicity itself, and all in six lines of golden code.

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.