Readers like you help support How-To Geek. When you make a purchase using links on our site, we may earn an affiliate commission. Read More.

Quick Links

Key Takeaways

  • The "yes" command in Linux and macOS can repeatedly generate any message you choose, such as "yes" or "no" strings.
  • The output from the "yes" command can be piped into other programs or scripts to provide automatic responses.
  • The "yes" command can also be used to generate large files quickly by redirecting its output into 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 though you were typing "y" and hitting Enter, over and over (and over and over) again. Very quickly. And it will carry on doing so until you press Ctrl+C to interrupt it.

yes

output from yes in a terminal window

In fact, yes can be used to repeatedly generate any message you choose. Simply type yes, a space, the string you wish 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

output from yes yes in a terminal window

yes anything you like

output from yes with a line of text in a terminal window

But What Use Is That?

The output from yes can be piped into other programs or scripts.

Does this sound familiar? You start a long process running and step away, leaving it to run. When you return to your computer, the process hasn't completed at all. In your absence, it has asked you a question and is sat waiting for a "yes" or "no" response.

If you know in advance that all your answers are going to be positive ("yes" or "y") or negative ("no" or "n") you can use yes to provide those responses for you. Your long process will then run through to completion unattended with yes providing the answers to any questions the process asks.

Using yes With Scripts

Look at the following Bash shell script. (We need to imagine that this is a part of a much larger script that will take a considerable 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 awaits a response. The logic flow within the script is decided upon by the input from the user.

  • A "yes" or "y" indicates a positive response.
  • Any other input is considered a negative response.
  • Pressing Enter with no input 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 anything else as input, including pressing Enter with no input text.

./long_script.sh

output from long_script.sh in a terminal window

To get yes to provide our response to the script's question, pipe the output from yes to the script.

yes | ./long_script.sh

piping yes into long_script.sh in a terminal window

Some scripts are more rigid in their requirements and only accept the full word "yes" as a positive response. You can provide "yes" as a parameter to yes, as follows:

yes yes | ./long_script.sh

piping yes yes into long_script.sh in a terminal window

Don't Say yes Without Thinking It Through

You need to be certain that the input you are going to feed into the script or program is definitely going to give you the outcome you expect. To be able to make that decision, you must know the questions and what your responses should be.

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

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

Using yes With Commands

In its infancy, yes would be used with other Linux commands. Since then, most of those other Linux commands have their own way of running without human interaction. yes is no longer required to achieve that.

Let's take the Ubuntu package manager apt-get as an example. To install an application without having to press "y" half-way through the installation, yes would have been used as follows:

yes | sudo apt-get install fortune-mod

piping yes into apt-get in a terminal window

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

sudo apt-get -y install fortune-mod

using apt-get with the assume yes option in a terminal window

You'll see that apt-get didn't even ask its usual "Do you want to continue? [Y/n]" question. It 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 one time:

yes | yum install fortune-mod

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

dnf -y install fortune-mod

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

So does it seem that yes has been relegated to only working with scripts? Not quite. There are a few more tricks in the old dog yet.

Some Further yes Tricks

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

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

Instead of simply echoing the digits to the terminal window, you could call another command or script. That command or script doesn't even need to use the digits, and they're only there to kick-start each cycle of the loop.

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

yes one-liner regulating a loop in the terminal window

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

You can rapidly generate large files with yes. All you need to do is give it a long string of text to work with and redirect the output into a file. Make no mistake; those 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 i a terminal window

The file generated here took about five seconds on the test machine used to research this article. ls reports that it is 557 Mb in size, and wc tell us there are 12.4 million lines in it.

We can limit the size of the file by including head in our command string. We tell it how many lines to include in the file. The -50 means head will let just 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 i a terminal window

As soon as there are 50 lines in the test.txt file, the process will stop. You don't need to use Ctrl+C. It comes to a graceful halt on its own volition.

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

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—released in System 7 Unix and authored by Ken Thompson—amounts to a mere six lines of code. Even though it is still useful for feeding responses into long-running scripts (and a few other tricks), the yes command isn't going to be a part of your daily toolkit of commands. But when you do need it, you'll find it is simplicity itself—and all in six lines of golden code.

Linux Commands

Files

tar · pv · cat · tac · chmod · grep · diff · sed · ar · man · pushd · popd · fsck · testdisk · seq · fd · pandoc · cd · $PATH · awk · join · jq · fold · uniq · journalctl · tail · stat · ls · fstab · echo · less · chgrp · chown · rev · look · strings · type · rename · zip · unzip · mount · umount · install · fdisk · mkfs · rm · rmdir · rsync · df · gpg · vi · nano · mkdir · du · ln · patch · convert · rclone · shred · srm · scp · gzip · chattr · cut · find · umask · wc · tr

Processes

alias · screen · top · nice · renice · progress · strace · systemd · tmux · chsh · history · at · batch · free · which · dmesg · chfn · usermod · ps · chroot · xargs · tty · pinky · lsof · vmstat · timeout · wall · yes · kill · sleep · sudo · su · time · groupadd · usermod · groups · lshw · shutdown · reboot · halt · poweroff · passwd · lscpu · crontab · date · bg · fg · pidof · nohup · pmap

Networking

netstat · ping · traceroute · ip · ss · whois · fail2ban · bmon · dig · finger · nmap · ftp · curl · wget · who · whoami · w · iptables · ssh-keygen · ufw · arping · firewalld