TIL: saying `yes` the Unix way
Nov 27, 2022
Alexander Junge
1 minute read

Today (well, earlier this week but let’s not be picky here) I learned about the Unix core utility yes. It works like this:

$ yes
y
y
y
# [ad infinitum until stopped]
$ yes no
no
no
no
# [ad infinitum until stopped]

As you can see, yes either prints y\n until stopped or whatever string you pass it followed by a newline, again continuing until the process is killed.

What is this good for?

At first, I had no clear idea what yes could be used for but after some research, the following use cases appeared:

  • repeatedly answer yes (or no, or whatever) via stdin to commands that require user input, e.g., to rm *.json or apt install something
  • write repeated lines to a file $ yes "Hello test" | head -n 100 > test.txt
  • getting CPU load to 100% for load testing - essentially yes is just a while True loop

Consider adding yes to your toolbelt, if this sounds useful. Thanks for reading and let me know if you found other good use cases for yes.