قالب وردپرس درنا توس
Home / Tips and Tricks / Using the yes command on Linux

Using the yes command on Linux

The yes command seems too simple to be of practical use, but in this tutorial we'll show you how to use it and how you can benefit from its pent-up positivity on Linux and macOS.

The yes Command [1
9659003] The yes command is one of the simplest commands on Linux and other Unix-like operating systems, such as macOS. And by simply we just mean in its use and its initial implementation. The source code for the original release – released in System 7 Unix and authored by Ken Thompson – consists of only six lines of code.

But do not write it down to be a simple little command. It can be used in interesting and useful ways.

What does yes do?

Without command-line parameters, the yes command behaves as if you were typing "y" and pressing the Enter key over and over. Very fast. This will continue until you press Ctrl + C to pause it.


  yes output in a terminal window

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

  yes yes 

  Yes yes output in a terminal window

  Yes, all you want 

  Yes output 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 good is that?

The output of yes may be piped into other programs or scripts.

Does that sound familiar to you? They start a long process, step back and let it run. When you return to your computer, the process is not complete. In your absence, she has asked you a question and is waiting for an answer with "yes" or "no".

If you know in advance that all your answers will be positive ("yes" or "yes")) or negative ("no" or "n") you can use yes to this Provide answers for you. Your long process is then completed unattended, with yes providing the answers to all the questions asked by the process.

Using yes with scripts

Note the following bash shell script. (We have to imagine that this is part of a much larger script that takes a considerable amount of time to complete.)

  #! / Bin / bash

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

Echo "Are you happy to continue? [y,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" == "y" ]] || [[ "$input" == "yes" ]]; then

echo "Positive answer: $ input"

# treat anything but a negative answer

echo "negative answer: $ input"


This script asks a question and waits for an answer. The logical flow within the script is determined by the user's input.

  • A "Yes" or "Y" indicates a positive response.
  • Any other input is considered a negative response.
  • Pressing Enter with No input text will not work.

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 specifying "yes," "y," and anything else as input, including pressing Enter without any input 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;;

To get yes to answer the script question, forward the output of yes to the script. [19659006] yes | ./long_script.sh[19659021[19459032[pipingyesinlong_scriptshineinemTerminalfenster"width="646"height="122"src="/pagespeed_static/1JiBnMqyl6Sgif"onload="pagespeedlazyLoadImagesloadIfVisibleAndMaybeBeacon(this);"onerror="thisonerror=null;pagespeedlazyLoadImagesloadIfVisibleAndMaybeBeacon(this);”/>

Some scripts are stricter in their requirements and accept only the full word "yes" positive answer.You can specify as a parameter for yes :

  yes yes | ./long_script.sh[19659021[19459034[pipingyesyesinlong_scriptshineinemTerminalfenster"width="646"height="122"src="/pagespeed_static/1JiBnMqyl6Sgif"onload="pagespeedlazyLoadImagesloadIfVisibleAndMaybeBeacon(this);"onerror="thisonerror=null;pagespeedlazyLoadImagesloadIfVisibleAndMaybeBeacon(this);"/> 

Do not Say Yes Without Thinking

The input you type into the script or program will definitely give you the result you expect. To make that decision, you need to know the questions and your answers.

The logic in the script, command, or program may not meet your expectations. In our sample script, the question might be, "Would you like to stop? [y,n]. If that had been the case, a negative answer would have allowed the script to continue.

You need to be familiar with the script, command, or program before you quickly pipe yes into it.

Using yes With Commands

In its infancy, yes was used with other Linux commands. Since then, most of these other Linux commands have their own way of doing without human interaction. yes is no longer needed to accomplish this.

Take as an example the Ubuntu Package Manager apt-get . To install an application without having to press "y" half way through the installation, yes was used as follows:

  yes | sudo apt-get install fortune-mod 

The same result can be obtained with

. (suppose yes) Option in apt-get :

  sudo apt-get -y install fortune-mod 

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

You will see that apt-get did not even ask the usual," Do you want to proceed? [Y/n] "in question it was only assumed that the answer was "yes."

The situation is the same for other Linux distributions, but Fedora would have used this type of package manager command:

   yes | yum    install  dnf  package manager has replaced  yum  and  dnf  has his own option  -y  (assumed yes). 

   dnf -y    install    fortune-mod  

The same applies to cp fsck and rm [Thesecommandseachhavetheirown -f (force) or -y (accept yes) options.

So s does it seem that yes descended only to work with scripts? Not quite. There are a few more tricks in the old dog.

Some More Yes Tricks

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

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

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

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

 yes one-liners that regulate 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's useful to have a large file for testing. You may want to practice using the zip command, or you may want to have a large file to test FTP uploads.

With yes you can quickly generate large files. All you have to do is give it a long text string to work with and redirect the output to a file. Do not make a mistake; These files will grow fast. Within a few seconds, press Ctrl + C.

  yes long line with meaningless text to populate files> test.txt 
  ls -lh test.txt 
  wc test.txt 

 Generate test files with yes ia terminal window "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 computer on which this article was researched. ls indicates that it is 557 MB in size, and wc indicates that 12.4 million lines are included.

We can limit the size of the file by placing head on our command line. We indicate how many lines the file should contain. The -50 means that head passes only 50 lines into the file test.txt .

  yes, long line with meaningless text to populate files | head -50> test.txt 

 Using head to limit the size of a file, eg. Eg 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 there are 50 lines in

file, the process is stopped. You do not have to use Ctrl + C. It comes to a dignified standstill on your own.

wc reports that the file contains exactly 50 lines, 400 words and 2350 bytes in size.

Even if it still exists The yes command is useful for inserting responses into long-running scripts (and a few other tricks). It will not be part of your daily command tool. But when you need it, you'll find it's easy - all in six lines of golden code.

Source link