How to use the yes command on Linux

The yes command seems too simple to be of practical use, but in this tutorial we’re going to show you how to use it and how you can take advantage of its pent-up positivity on Linux and macOS.

The yes command

the yes Command is one of the simplest commands in Linux and other Unix-like operating systems like macOS. And by simple, we mean simple in its usage and initial implementation. The source code for the original version – published in System 7 Unix and written by Ken Thompson– is only six lines of code.

But don’t copy it because it’s a simple little command. It can be used in interesting and useful ways.

What is it doing?

Used with no command line parameters, the yes Command behaves as if you type “y” and press Enter, over and over again (and over and over). Very fast. This will continue until you press Ctrl + C to pause it.

yes

Actually, yes can be used to repeatedly generate any message. Just type yes, a space, the string you want to use, and then press Enter. This is often used to cause yes to generate an output stream of “yes” or “no” strings.

yes yes

yes anything you like

But what use is that?

The output of yes can be directed into other programs or scripts.


Does that sound familiar to you? You start a long process, then step back and let it run. When you return to your computer, the process is not yet complete. In your absence, it has asked you a question and is waiting for a “yes” or “no” answer.

If you know in advance that all of your answers will be positive (“yes” or “y”) or negative (“no” or “n”), you can yes to provide you with these answers. Your long process then runs unattended yes Providing answers to any questions the process asks.

Yes to use with scripts

Take a 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 amount of time to run.)

#!/bin/bash

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

echo "Are you happy to proceed? [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 response: $input"

# treat anything else as a negative response
else

   echo "negative response: $input"

fi

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

  • A “yes” or “y” indicates a positive answer.
  • Any other input counts as a negative answer.
  • To press Enter without input text does nothing.

To test this, copy the script to a file and save it as long_script.sh. Use chmod to make executable.

chmod +x long_script.sh


Run the script with the following command. Type “yes”, “y” and anything else as input, including pressing Enter without input text.

./long_script.sh

Receive yes To give our answer to the script’s question, derive the output from yes to the script.

yes | ./long_script.sh

Some scripts are stricter in their requirements and only accept the full word “yes” as a positive answer. You can specify “yes” as a parameter to yes, as follows:

yes yes | ./long_script.sh

Don’t say yes without thinking about it

You need to be sure that the input you put into the script or program will definitely produce the result you expect. In order to make this decision, you need to know the questions and your answers.

The logic in the script, command, or program may not be what you expected. In our example Script could have been the question, “Do you want to quit? [y,n]. ”If it had, a negative answer would have allowed the script to continue.

You must be familiar with the script, command, or program before you whistle yes into it.

Yes to use with commands

In its infancy yes would be used with other Linux commands. Since then, most of these other Linux commands have run without human interaction. yes is no longer required for this.


Take the Ubuntu package manager apt-get as a example. To install an application without having to press “y” halfway through the installation, yes would have been used like this:

yes | sudo apt-get install fortune-mod

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

sudo apt-get -y install fortune-mod

You’ll see apt-get didn’t even ask his usual “Do you want to continue?” [Y/n]”Question. The answer was simply assumed to be” yes “.

The situation is the same with other Linux distributions. on Fedora You would have used this type of package manager command all at once:

yes | yum install fortune-mod

the dnf Package manager has replaced yum and dnf has its own -y (assumed yes) option.

dnf -y install fortune-mod


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

It seems like it is yes was reference made to working with scripts? Not quite. There are a few more tricks on the old dog.

Some more yes tricks

You can use yes with a sequence of digits 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 a second.

Instead of simply sending the digits to the terminal window, you can invoke another command or script. That command or script doesn’t even have to use the digits, and they’re only there to start each cycle of the loop.

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 is useful to have a large file to test. You may want to practice using the zip command or have a large file to test FTP uploads.


You can quickly create 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 rapidly. Be ready to press Ctrl + C within 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 yes ia terminal window

The file generated here took about five seconds on the test computer used to research this article. ls reported that it is 557MB in size, and wc Tell us it has 12.4 million lines.

We can limit the size of the file by adding head in our command string. We tell it how many lines to include in the file. the -50 means head only lets 50 lines through to the test.txt File.

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

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

As soon as 50 lines in. are test.txt File will terminate the process. You don’t have to use Ctrl + C. It comes to a graceful hold by itself.

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

While it’s still useful for typing answers into long running scripts (and some other tricks), this is the yes Command will not be part of your daily command toolkit. But when you need it, you’ll find it’s easy – all in six lines of golden code.

Linux commands
Filestar · pv · cat · tac · chmod · grep · difference · sed · With · man · pushed · popd · fsck · Test disk · seq · fd · pandoc · CD · $ PATH · awk · join · jq · wrinkles · unique · Journalctl · tail · stat · ls · fstab · echo · fewer · chgrp · chown · rev · look · Strings · Type · rename · Postal code · unzip · assemble · ummount · To install · fdisk · mkfs · rm · rmdir · rsync · df · gpg · weather · Nano · mkdir · from · ln · Patch · Convert · rclon · Scraps · srm
Processesalias · screen · above · kind · renice · progress · strace · system · tmux · chsh · story · at · Batch · for free · which · dmesg · chfn · User mod · ps · chroot · xargs · tty · pinkie finger · lsof · vmstat · Time out · Wall · Yes sir · kill · sleep · sudo · it is · Time · groupadd · User mod · groups · lshw · switch off · start anew · Stop · switch off · passwd · lscpu · crontab · date · bg · fg
Networkingnetstat · Ring · Trace route · ip · ss · who is · fail2ban · bmon · she · finger · nmap · ftp · curl · wget · who · who am I · w · iptables · ssh-keygen · ufw

Best Linux Laptops for Developers and Enthusiasts

Related Posts