Before diving in it’s worth mentioning that my command line experience is very much limited to running a few git commands or executing a local server – it’s beginners to say the least – so do read the following contents with that in mind!

A little bit about yes: when run, yes prints out a specified string and a new line as a continuous stream until stopped. If no string is given then it prints out ‘y’ by default:

yes “All work and no play…”
All work and no play…
All work and no play…
All work and no play…
All work and no play…
All work and no play…

You get the gist. Not useful at all on its own (unless your name is Jack Torrance) but when piped in to other commands it’ll provide an automatic response to any command that might require one and then stop once the chain is complete.

For example, I use CleanShot for screenshotting (it’s great – I highly recommend it). Every now and then I use the command line to clean out all the screenshots from my Desktop. I typically run this command: rm -i "CleanShot"*.png (I use -i to provide a confirmation for each screenshot just in case I delete the wrong file). Now, if I’m feeling bold, I can pipe this command after yes and it will handle all of the confirmation for me: yes | rm -i "CleanShot"*.png (you can also achieve the same thing by using the force -f flag on the rm command instead for the same effect).

So what does this have to do with testing the charge on a MacBook Air battery? Well it’s actually the continuous stream side effect I mentioned earlier that we’re interested in. I’d replaced the battery and in order to test that it was working as expected, I wanted to take it through a charge, drain and then a final recharge cycle. To drain the battery as quickly as possible maxing out the CPU load is your best bet. This is where reaching for yes comes in to play.

Here is the command to run:

yes > /dev/null &

This command is doing a few things:

  • yes: we get a continuous stream of y as fast as the CPU can manage
  • > /dev/null: we’re redirecting the output of yes and putting it in to /dev/null which, for Unix systems, is instantly thrown out (also known as the black hole)
  • &: this pushes the process in to the background so you can continue using the command line

By opening up the Activity Monitor on your Mac you’ll see your CPU load increasing. If your CPU has multiple cores (and it most likely does) you will want to run that same command a few times to get to 100% load. This worked wonders and got the battery from around 7 hours of charge time down to 15 minutes!

To finish things up, when the battery had dropped to around 5%, to avoid a forced shutdown I ran killall yes which terminates the command completely.

And that’s it! Job well done in my book. Anyway, the following video sums up ‘yes’ in a nutshell if you’re interested: