- What does the yes command do?
- Don’t say yes without thinking
- The “yes” command in Linux and macOS can repeatedly generate any message you want, such as “yes” or “no” strings.
- The output of the “yes” command may be redirected to other programs or scripts to provide automatic responses.
- The “yes” command can also be used to quickly generate large files by redirecting its output to a file.
The yes command seems too simple to be of any practical use, but in this tutorial we’ll show you its application and how to benefit from its pent-up positivity in Linux and macOS.
What does the yes command do?
Used without any command line parameters, the yes command behaves as if you were typing “y” and hitting Enter, over and over (and over and over). Very quickly. And it will continue like this until you press Ctrl+C to interrupt it.
In fact, yes can be used to repeatedly generate whatever message you want. Simply type yes, a space, the string you want to use, and then press Enter. This is often used to make yes generate an output stream of “yes” or “no” strings.
yes anything you like
But what is that for ?
The output of yes can be redirected to other programs or scripts.
Does this sound familiar? You start a long process and walk away, letting it run. When you return to your computer, the process is not finished at all. In your absence, he asked you a question and expects a “yes” or “no” answer.
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 those answers for you. Your lengthy process will then continue to completion unsupervised, with the yes providing the answers to all questions posed 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 considerable time to execute.)
echo "Are you happy to proceed? (y,n)"
if ( "$input" == "" ); then
echo "Nothing was entered by the user"
elif (( "$input" == "y" )) || (( "$input" == "yes" )); then
echo "Positive response: $input"
echo "negative response: $input"
This script asks a question and expects a response. The logical flow in the script is decided by user input.
- A “yes” or “y” indicates a positive response.
- Any other entry is considered a negative response.
- Pressing Enter without entering any 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 providing “yes”, “y”, and any other input, including pressing Enter without typing any text.
For yes to provide our answer to the question from the script, pipe the output of yes to the script.
yes | ./long_script.sh
Some scripts are more rigid in their requirements and only accept the full word “yes” as a positive answer. You can provide “yes” as a parameter for yes, as follows:
yes yes | ./long_script.sh
Don’t say yes without thinking
You need to be sure that the input you are going to introduce into the script or program will definitely give you the result you expect. To be able to make this decision, you need to know the questions and what your answers should be.
The logic of the script, command, or program may not match your expectations. In our example script, the question could have been “Do you want to stop?” (we). » If this had been the case, a negative response would have allowed the scenario to continue.
You must be familiar with the script, command, or program before blithely launching
yes in it.
Use yes with commands
At its beginnings,
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 necessary to achieve this.
Let’s take the Ubuntu package manager
apt-get For example. To install an app without having to press “y” halfway through the installation,
yes would have been used as follows:
yes | sudo apt-get install fortune-mod
The same result can be obtained using the
-y (let’s assume yes) option in
sudo apt-get -y install fortune-mod
You will see that
apt-get didn’t even ask his usual question “Do you want to continue? (Y/n)”. 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 some point:
yes | yum install fortune-mod
dnf the package manager replaced
dnf has its own
-y (let’s assume yes) option.
dnf -y install fortune-mod
The same applies to
rm. These commands each have their own
-f (force) or
-y (let’s assume yes) options.
It therefore seems that
yes been relegated to working only with scripts? Not enough. There are still some tricks in the old dog.
Some other tips yes
You can use
yes with a sequence of numbers generated by
seq to control a loop of repeated actions.
This one-liner echoes the numbers generated in the terminal window, then calls
sleep for a second.
Instead of just returning the numbers to the terminal window, you can call another command or script. This command or script doesn’t even need to use numbers, and they are only there to start each cycle of the loop.
yes "$(seq 1 20)" | while read digit; do echo digit; sleep 1; done
Sometimes it helps to have a large file to test with. Perhaps you want to practice using the zip command or have a large file with which to test FTP uploads.
You can quickly generate large files with
yes. All you have to do is give it a long string of text to work with and redirect the output to a file. Don’t make a mistake; these files will grow quickly. Be ready to press Ctrl+C in a few seconds.
yes long line of meaningless text for file padding > test.txt
ls -lh test.txt
The file generated here took about five seconds on the test machine used to research this article.
ls reports that it is 557 MB, and
wc tell us it contains 12.4 million rows.
We can limit the file size by including
head in our chain of command. We tell it how many lines to include in the file. THE
head will only allow 50 lines to pass through
yes long line of meaningless text for file padding | head -50 > test.txt
As soon as there are 50 lines in the
test.txt file, the process will stop. You don’t need to use Ctrl+C. He stops gracefully of his own accord.
wc reports that there are exactly 50 lines in the file, 400 words and its size is 2350 bytes.
The yes command is one of the simplest commands in Linux and other Unix-like operating systems like macOS. The source code for the original version, published under System 7 Unix and written by Ken Thompson, represents only six lines of code. While still useful for inserting responses into long-running scripts (and a few other tricks), the
yes The command will not be part of your daily toolbox of commands. But when you need it, you’ll find it’s simplicity itself, and all in six lines of golden code.