Skip to content
Tricks & Tips Search the site/

The yes command on Mac and Linux: what it's for

What the yes command prints, how to stop it, and its real uses - answering prompts, making test files and loading the CPU on Mac and Linux.

Updated 5 min read

The quick answer

The yes command prints "y" on a new line, over and over, until you stop it. Give it a word or a sentence (yes hello) and it repeats that instead. Press Ctrl+C to stop it, or pipe it into head -n 5 to get just five lines.

Its real job is feeding answers to a command that keeps asking questions: yes | command answers "y" to every prompt and yes n | command answers "n". Most modern tools have their own -y or -f flag for this, which is usually the cleaner choice.

The yes command looks like a joke the first time you see it in a script or a forum answer: all it does is print y over and over. It’s built into macOS and most Linux systems, and it’s about as simple as a program gets. Its uses are small but real, and most of them come from piping its output into another command.

What the yes command does

The yes command prints the letter y followed by a new line, again and again, until you stop it.

Open Terminal on a Mac (or any terminal on Linux), type yes, and your screen fills with a column of y’s that never ends. Give it something to say and it repeats that instead:

yes hello world

That prints “hello world” on every line. That’s the whole program. It needs no input and never finishes on its own, which sounds pointless until you connect it to another command that wants the same answer over and over.

How to stop yes

Press Ctrl+C in the same Terminal window to stop yes right away.

If you only want a few lines, pipe it into head:

yes | head -n 3

This prints three y’s and gives you the prompt back. head quits once it has its lines, and yes stops the next time it tries to write. Most uses below work this way: yes supplies the lines, and the command after it decides what to do with them.

Answer a command’s prompts with yes

The main use of yes is answering a command that asks the same question over and over, so you don’t have to type “y” a hundred times.

Put yes | in front of the command, and each time the command asks, it reads the next line from yes:

yes | command

To answer no to everything instead, give yes the letter n. Say you’re copying photos from Downloads into Pictures and don’t want to overwrite any that are already there:

yes n | cp -i ~/Downloads/*.jpg ~/Pictures/

cp -i asks before overwriting each existing photo, and yes n says no every time, so only the new photos get copied.

The catch is that yes answers without reading. Pipe it into something that deletes, such as yes | rm -i *, and it approves every deletion before you can read the questions scrolling past. It can’t type passwords either, because sudo and ssh read those from the terminal itself, not from the pipe.

Use the command’s own flag when it has one

Most tools that ask for confirmation have their own switch to turn the questions off, and that’s usually cleaner than yes. On Debian and Ubuntu, apt-get -y runs without asking, rm -f removes files without asking, and the Linux disk checker e2fsck -y answers yes to every repair question. Reach for yes when a tool has no such flag, or when you want to answer “n” to everything.

Fill a file with test lines

yes plus head is the quickest way to make a file with a set number of identical lines.

yes "This is a test line" | head -n 1000 > test.txt

That creates test.txt with exactly 1,000 lines, which is handy for trying out a text editor, a sort command or an upload on a file of known size. Plain yes lines are two bytes each, a y and a newline, so a million of them make a 2 MB file.

Max out the CPU with yes > /dev/null

Running yes > /dev/null keeps one processor core busy at close to 100%, because yes writes as fast as it can and /dev/null throws it all away.

That makes it a quick way to test cooling, check that the fans spin up or see how a laptop behaves under load. One copy only loads one core, though, so to load the whole processor you start one per core. On a Mac, sysctl -n hw.ncpu gives the number of processors to load (on Apple silicon that’s the core count):

for i in $(seq $(sysctl -n hw.ncpu)); do yes > /dev/null & done

On Linux, nproc does the same job:

for i in $(seq $(nproc)); do yes > /dev/null & done

The & sends each copy to the background, so you get your prompt back. On a Mac you can watch the load in Activity Monitor’s CPU tab, and on Linux with top. When you’re done, stop every copy at once:

pkill yes

Don’t leave them running on a laptop on battery. They drain it fast and make the machine hot.

yes on a Mac vs. yes on Linux

Both versions print the same thing; the difference is that the Linux version has two options and the Mac version has none.

Linux uses yes from GNU coreutils, which understands yes --help and yes --version. The Mac’s yes comes from BSD and has no options at all, so yes --help on a Mac just prints --help until you press Ctrl+C. That only matters if you want to repeat a line that starts with a dash. On Linux, put -- in front: yes -- --help prints --help forever.

Common questions

How do I stop the yes command?

Press Ctrl+C in the Terminal window where it is running. If it is running in the background, type pkill yes, which works on both Mac and Linux.

Why would anyone need the yes command?

It answers repeated confirmation prompts for you. Piping yes into a command that asks "Are you sure?" for every file answers y each time, and yes n answers no each time. It is also handy for making test files and for keeping a CPU core busy.

Can yes print something other than y?

Yes. Whatever you type after it gets repeated. yes hello world prints "hello world" on every line, and yes n prints n.

Is the yes command dangerous?

Only through what you pipe it into. On its own it just prints text and uses one CPU core until you stop it. Pointed at a command that deletes or overwrites files, it agrees to every prompt without you reading them.

Is yes the same on Mac and Linux?

It does the same job on both. The Linux version (GNU coreutils) also understands --help and --version. The Mac version has no options, so yes --help just prints "--help" forever.

Sources

Keep exploring

Search tricksandtips.net