There is a yes command in unix/linux which basically infinitely prints y to the stdout. What is the point of it, and how is it useful?
- A few extra possibilities here.user44370– user443702013-11-25 14:09:20 +00:00Commented Nov 25, 2013 at 14:09
- 1possible duplicate of What is the point of the `yes` command?... (Flagging this as duplicate because the other newer question is better asked and better answered).Aditya– Aditya2013-11-25 15:14:26 +00:00Commented Nov 25, 2013 at 15:14
- Relating: unix.stackexchange.com/q/257297/117549Jeff Schaller– Jeff Schaller ♦2017-11-10 17:59:59 +00:00Commented Nov 10, 2017 at 17:59
- matthias-endler.de/2017/yesslm– slm ♦2018-01-16 19:31:12 +00:00Commented Jan 16, 2018 at 19:31
6 Answers
yes can be used to send an affirmative (or negative; e.g. yes n) response to any command that would otherwise request one, thereby causing the command to run non-interactively.
The yes command in conjunction with the head command can be used to generate large volume files for means of testing.
It can also be used to test how well a system handles high loads, as using yes results in 100% processor usage, for systems with a single processor (for a multiprocessor system, a process must be run for each processor). This, for example, can be useful for investigating whether a system's cooling system will be effective when the processor is running at 100%.
In 2006, the yes command received publicity for being a means to test whether or not a user's MacBook is affected by the Intermittent Shutdown Syndrome. By running the yes command twice via Terminal under Mac OS X, users were able to max out their computer's CPU, and thus see if the failure was heat related
via wikipedia: http://en.wikipedia.org/wiki/Yes_(Unix)
- 13For extra damage, try
yes `yes yes`sendmoreinfo– sendmoreinfo2013-04-21 13:49:34 +00:00Commented Apr 21, 2013 at 13:49 - 4@sendmoreinfo EvilTobias Kienzler– Tobias Kienzler2013-11-13 09:12:28 +00:00Commented Nov 13, 2013 at 9:12
- ohh, thats a nice stress test. just tried it, then ran to my colleague asking for a new vm xDToastgeraet– Toastgeraet2019-07-04 13:26:26 +00:00Commented Jul 4, 2019 at 13:26
This might be a controversial opinion, but in my view it is an ugly fix for bad user interface in command-line tools.
Some command-line tools ask questions to the user with a prompt and do not have an option to run non-interactively; imagine for instance something such as
$ frobnicate * frobnicate file a.txt? (y/n) y frobnicate file b.txt? (y/n) y ... Since the answer to the question is taken from standard input, a quick fix to this problem is having an application that outputs the string y\n continuously, which is exactly what yes does. Unix pipes can be used to send this output as an input to a given command.
$ yes | frobnicate One of the issues with this approach is that yes has no possibility to check the question it is answering to:
frobnicate file a.txt? (y/n) y frobnicate file b.txt? (y/n) y format device /dev/sda1? (y/n) y frobnicate file c.txt? (y/n) y A better solution, when it is available, is a specific option to run non-interactively, such as rm -f or apt-get -y. This allows coding more sensible behavior in the application.
- 1I read all the other answers. I agree with yours completely w.r.t. ugly fix...though it does have a valid use for testing computer's response to high cpu loads.Wildcard– Wildcard2015-10-15 02:59:05 +00:00Commented Oct 15, 2015 at 2:59
- 13Of course, you could use the proprietary software "Microsoft Windows" to test computer response to high cpu loads, but "yes" is open source.Wildcard– Wildcard2015-10-15 03:00:01 +00:00Commented Oct 15, 2015 at 3:00
- 2It's one of the most beautiful ugly fixes thoughJezzamon– Jezzamon2018-02-09 00:04:53 +00:00Commented Feb 9, 2018 at 0:04
I like to use yes when I am unzipping multiple .zip files that all contain the same file with the same name and I am asked what to do in each case (ex. a licence agreement).
yes | for z in *.zip; do unzip "$z"; done - Arguably, the -u or -n options to unzip would accomplish much the same thing, but I can definitely see and agree how a generic and easily-memorable solution that works across a wide number of tasks beats trying to memorize a specific solution just for unzip, or worse, looking it up in the man page every darn time.Dewi Morgan– Dewi Morgan2023-07-06 17:48:01 +00:00Commented Jul 6, 2023 at 17:48
Just came across another use for it: liked to wipe out a hard disk with a different pattern than just zeroes (like: dd if=/dev/zero of=/dev/sdd bs=1M) and used 'yes' for it:
yes UUUUUUUUUUUUU > /dev/sdd
- 1Beware though that it does fill up the disk with
Us but withUUUUUUUUUUUUU\ns. To fill up withUs, you'd needyes '' | tr '\n' U > /dev/sddortr '\0' U < /dev/zero > /dev/sddStéphane Chazelas– Stéphane Chazelas2019-11-27 08:37:10 +00:00Commented Nov 27, 2019 at 8:37
A coworker used this in a novel way to input a password
yes password | passwd -
passwdwill ask for the old password first, if the user is not root or a superuser and will not continue unless the correct password is entered. So either the user is root orpasswordis the old password as well and nothing is changed.Michael Konietzka– Michael Konietzka2013-11-10 16:25:47 +00:00Commented Nov 10, 2013 at 16:25 - @MichaelKonietzka indeed. But with the conditions you mentioned it's a useful use case still.frogstarr78– frogstarr782013-11-16 06:00:19 +00:00Commented Nov 16, 2013 at 6:00
- 5Except this would persist in history in plaintext. Crazeyyy.Gleno– Gleno2014-08-16 19:20:29 +00:00Commented Aug 16, 2014 at 19:20
- 5@Gleno in bash, you can precede it with a space to prevent it from being recorded (as in stackoverflow.com/q/640403/118153)Iiridayn– Iiridayn2014-12-08 08:36:25 +00:00Commented Dec 8, 2014 at 8:36
- 4Also, this leaks the password to all local users in the process list (
psortopat the right moment).Federico Poloni– Federico Poloni2016-01-24 09:00:15 +00:00Commented Jan 24, 2016 at 9:00
E.g. use curl as ping
yes https://unix.stackexchange.com | xargs curl -I -vsS --connect-timeout 60