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.
The yes Command
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 use and its initial implementation. 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.
But don’t write it off for being a simple little command. It can be used in some interesting and useful ways.
What Does yes 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 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 anything you like
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
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.
yes to provide our response to the script’s question, pipe the output from
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 response. You can provide “yes” as a parameter to
yes, as follows:
yes yes | ./long_script.sh
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
The same result can be achieved using the
-y (assume yes) option in
sudo apt-get -y install fortune-mod
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
dnf package manager has replaced
dnf has its own
-y (assume yes) option.
dnf -y install fortune-mod
The same applies to
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
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
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
head will let just 50 lines through to the
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. 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.
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.
|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|
|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|
|Networking||netstat · ping · traceroute · ip · ss · whois · fail2ban · bmon · dig · finger · nmap · ftp · curl · wget · who · whoami · w · iptables · ssh-keygen · ufw|
- › How to Use the “yes” Command on a Mac
- › How to Use the rev Command on Linux
- › Logitech MX Master 3S Mouse Review: Muted Refinements
- › Should You Buy a Drone?
- › The Best Budget Speakers of 2022
- › Logitech MX Mechanical Keyboard Review: Easy on the Eyes, Not the Fingertips
- › What’s New in Chrome 102, Available Now
- › 4 Useless Tech Products You Shouldn’t Buy