How to Use the yes Command on Linux

The yes command seems too simple for practical use, but in this tutorial, we will show you its application and how to take advantage of its positivity remade under Linux and macOS.

The command yes

The yes command is one of the simplest commands in Linux and others. Unix-like operating systems like macOS. And by simple, we mean simple in its use and its initial implementation. The source code of the original version, published in System 7 Unix and created by Ken Thompson– equals a simple six lines of code.

But do not write it for a simple small order. It can be used in an interesting and useful way.

What does yes?

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

Yes

exit yes in a terminal window

In fact, yes can be used to repeatedly generate the message of your choice. Just type yes, a space, the string you want to use, and then press Enter. This is often used to make sure that yes generates an output stream of "yes" or "no" strings.

Yes Yes

exit yes yes in a terminal window

yes everything you like

output of yes with a line of text in a terminal window "width =" 646 "height =" 382 "src =" / pagespeed_static / 1.JiBnMqyl6S.gif "onload =" pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this); "onerror =" this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this);

But what is that for?

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

Sound familiar? You start a long process and you go away, letting it run. When you return to your computer, the process is not complete at all. In your absence, he has asked you a question and is waiting to answer "yes" or "no".

If you know in advance that all your answers will be positive ("yes" or "y") or negative ("no" or "n"), you can use yes to provide these answers for you. Your long process will then go to completion without supervision, with answers to all the questions asked by the process.

Use yes with scripts

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

#! / bin / bash

# …
# in the middle of a long script
# get a response from the user
# …

echo "Are you happy to proceed? [y,n]"
read the entry

# did we get an entry value?
if [ “$input” == “” ]; then

echo "Nothing has been entered by the user"

# Was it a y or a yes?
elif [[ “$input” == “y” ]]|| [[ “$input” == “yes” ]]; then

echo "Positive response: $ input"

# treat anything else like a negative answer
other

echo "negative answer: $ input"

Fi

This script asks a question and waits for an answer. The logical flow in the script is decided by the input of the user.

A "yes" or a "y" indicates a positive answer.
Any other entry is considered a negative answer.
Pressing Enter without entering the 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", "y" and anything else as input, including pressing Enter without text.

./long_script.sh

output of long_script.sh in a terminal window "width =" 646 "height =" 382 "src =" / pagespeed_static / 1.JiBnMqyl6S.gif "onload =" pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this); "onerror =" this. onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this);

For yes to provide our answer to the script question, pass the output of yes to the script.

yes | ./long_script.sh

piping yes in long_script.sh in a terminal window "width =" 646 "height =" 122 "src =" / pagespeed_static / 1.JiBnMqyl6S.gif "onload =" pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this); "onerror =" this .onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this);

Some scripts have stricter requirements and only accept the word "yes" as a positive answer. You can supply "yes" as a parameter to yes, as follows:

yes yes | ./long_script.sh

piping yes yes in long_script.sh in a terminal window "width =" 646 "height =" 122 "src =" / pagespeed_static / 1.JiBnMqyl6S.gif "onload =" pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this); "onerror" this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this);

Do not say yes without thinking about it

You must be certain that the input you are going to feed into the script or the program will definitely give you the result you expect. In order to make this decision, you must know the questions and know the answers.

The logic of the script, the command or the program may not match your expectations. In our example script, the question might have been, "Do you want to stop? [y,n]. If that had been the case, a negative answer would have allowed the script to continue.

You must be familiar with the script, command, or program before you can happily insert a yes.

Use yes with commands

In the beginning, yes would be used with other Linux commands. Since then, most of these other Linux commands have their own way of working without human interaction. yes is no longer needed to achieve it.

Take for example the Ubuntu package manager apt-get. To install an application without having to press "y" halfway through the installation, yes would have been used as follows:

yes | sudo apt-get install fortune-mod

piping yes in apt-get in a terminal window "width =" 646 "height =" 57 "src =" / pagespeed_static / 1.JiBnMqyl6S.gif "onload =" pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this); "onerror =" this .onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this);

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

sudo apt-get -y install fortune-mod

Use apt-get with the option assume yes in a terminal window "width =" 646 "height =" 382 "src =" / pagespeed_static / 1.JiBnMqyl6S.gif "onload =" pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this ); "onerror =" this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this);

You will see that apt-get did not even ask his usual "Do you want to continue? [Y/n]"Question." He 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 the same time:

yes | Yum install fortune-mod

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

dnf -y install fortune-mod

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

So, does it seem that yes has been relegated to work only with scripts? Not enough. There are still some tricks in the old dog.

Some other things yes

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

This line repeats the digits generated in the terminal window and then calls the standby for one second.

Instead of simply returning the digits to the terminal window, you can call another command or script. This command or script does not even need to use digits, which are only there to start each cycle of the loop.

yes "$ (seq 1 20)" | reading the number; echoing figure; to sleep 1; completed

yes one-liner regulating a loop in the terminal window "width =" 644 "height =" 307 "src =" / pagespeed_static / 1.JiBnMqyl6S.gif "onload =" pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this); "onerror =" this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this);

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

You can quickly generate large files with yes. All you have to do is give it a long string of text and redirect the output to a file. Do not make mistakes; these files will grow quickly. Be ready to press Ctrl + C in a few seconds.

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

generate test files with the terminal window yes "width =" 646 "height =" 212 "src =" / pagespeed_static / 1.JiBnMqyl6S.gif "onload =" pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this); " onerror = this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this);

The file generated here took about five seconds on the test machine used to find this article. According to our information, it has a size of 557 MB and tells us that it contains 12.4 million lines.

We can limit the file size by including head in our command string. We tell him how many lines to include in the file. The value -50 means that head will leave only 50 lines in the test.txt file.

yes long line of text without meaning for file filling | head -50> test.txt

use of head to limit the size of a file ia terminal window "width =" 646 "height =" 167 "src =" / pagespeed_static / 1.JiBnMqyl6S.gif "onload =" pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this ); "onerror =" this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this);

As soon as the test.txt file contains 50 lines, the process stops. You do not need to use Ctrl + C. It stops gracefully of its own free will.

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

While it may be useful for feeding long script responses (and some tips), the yes command will not be part of your daily toolbox. But when you need it, you'll find it's simplicity itself – and all in six golden lines of code.

Advertisements

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.