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
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 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, all you want
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 otherwise echo "negative answer: $ input" fi
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.
yes to answer the script question, forward the output of
yes to the script.  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 | ./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
Using yes With Commands
In its infancy,
yeswas used with other Linux commands. Since then, most of these other Linux commands have their own way of doing without human interaction.
yesis 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,
yeswas 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
You will see that
apt-getdid 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
dnfpackage manager has replaced
dnfhas his own option
-y(assumed yes).dnf -y install fortune-mod
The same applies to
-y(accept yes) options.
So s does it seem that
yesdescended only to work with scripts? Not quite. There are a few more tricks in the old dog.
Some More Yes Tricks
You can use
yeswith a number sequence generated by
seqto control a loop of repeated actions.
This one-liner returns the generated digits to the terminal window and then calls
sleepfor 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
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.
yesyou 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.txtls -lh test.txtwc test.txt
The file generated here took about five seconds on the test computer on which this article was researched.
lsindicates that it is 557 MB in size, and
wcindicates that 12.4 million lines are included.
We can limit the size of the file by placing
headon our command line. We indicate how many lines the file should contain. The
headpasses only 50 lines into the file
test.txt.yes, long line with meaningless text to populate files | head -50> test.txt
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.
wcreports that the file contains exactly 50 lines, 400 words and 2350 bytes in size.
Even if it still exists The
yescommand 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.