yes Command: Automating Confirmations

yes Command: Automating Confirmations

What You'll Learn

  • What the yes command actually does
  • How to auto-answer interactive prompts like "Are you sure? (y/n)"
  • Why yes can be dangerous and how to avoid trouble
  • Safer alternatives such as a command's own -y option

Quick Summary

  • Want to answer y to everything → yes | command
  • But prefer a command's own -y option when it has one (safer)
  • Stop yes with Ctrl + C

1. What Is the yes Command?

Conclusion: yes simply prints the letter y over and over, forever, until you stop it.

Lina: I heard there's a command literally called yes. What does it do? Does it say "yes" for you?
Linny-senpai: Good question. It is just like the name, but simpler than you think. It prints the letter y over and over, endlessly, until you stop it.
Lina: Endlessly? Let me try it right now.
Linny-senpai: Hold on. If you just run it, your screen fills with y and won't stop. Let me give you the stop key first: press Ctrl + C to stop it. Remember that, then try.

Here is what happens (stop it right away with Ctrl + C):

$ yes
y
y
y
y
(keeps going until you press Ctrl + C)

Running yes with no arguments prints y forever. Don't panic, just press Ctrl + C to stop. This is not a bug, it's how yes is supposed to work.

2. Why Is "Printing y Forever" Useful?

Conclusion: To feed that y automatically into commands that keep asking "Are you sure? (y/n)".

Lina: But what's the point of printing y forever?
Linny-senpai: On its own it looks like a prank. Its real value shows up when you combine it with a pipe (|).
Lina: A pipe... that's the "send the left output to the right command" thing we did before.
Linny-senpai: Exactly. Say you're deleting lots of files and rm asks "remove this? (y/n)" for each one. With 100 files, that's 100 times you type y. That's where yes comes in.

When yes shines

For commands that ask a confirmation prompt many times ("Are you sure? (y/n)"), yes supplies that y automatically, so you don't type it again and again.

3. Auto-Answer With a Pipe: yes | command

Conclusion: yes | command feeds an automatic y to every confirmation prompt the command shows.

For example, rm -i (interactive) asks before every deletion.

$ rm -i file1.txt file2.txt file3.txt
rm: remove regular file 'file1.txt'?

Instead of typing y each time, pipe yes into it and every prompt gets an automatic y.

$ yes | rm -i file1.txt file2.txt file3.txt
Lina: Just adding yes | at the front answers every prompt for me!
Linny-senpai: Right. yes keeps streaming y, and rm consumes one per file as it deletes. It works no matter how many prompts appear.

This is handy but also very dangerous. Skipping every confirmation means the wrong files get deleted with no questions asked. Always check what the command targets before you run it. We cover this in "The Danger of yes" below.

4. Repeat Any String: yes STRING

Conclusion: Give yes an argument and it repeats that string instead of y.

yes isn't limited to y. Pass an argument and it repeats that string.

$ yes hello
hello
hello
hello
(continues until Ctrl + C)

Multiple words are joined with spaces into one line.

$ yes I am ready
I am ready
I am ready
(continues until Ctrl + C)

Repeat empty lines (instead of mashing Enter)

yes "" repeats empty lines (just newlines). Useful for prompts where you only need to press Enter repeatedly.

$ yes "" | command

Conclusion: Pipe yes into head to take a fixed number of lines instead of an infinite stream. Handy for test data.

When infinite output is a problem, use head to take only the lines you need.

$ yes | head -n 5
y
y
y
y
y

The same works with a custom string. You get repeated text instantly.

$ yes test | head -n 3
test
test
test
Lina: So you cut off the infinite loop with head at "stop here".
Linny-senpai: Right. yes is also quietly useful for making dummy test data when checking how a command behaves. Use head -n COUNT to pick exactly how many lines you want.

6. The Danger of yes

Conclusion: yes skips every confirmation, so pairing it with irreversible operations like delete or overwrite invites disaster.

Lina: yes | seems like a magic fix. Why not use it for everything?
Linny-senpai: That's the key point. A confirmation prompt is the last safety net asking "are you really sure? this can't be undone." yes ignores all of them. So if it sweeps up a file you shouldn't have touched, it's gone for good.

Don't add yes | to a command you don't fully understand. When copy-pasting commands from the internet, be extra careful if yes | is at the front.

7. Safer Alternatives to yes

Conclusion: Many commands have their own -y / --yes option. Prefer it over piping yes.

If you only want to skip confirmations, you often don't need yes at all. The command itself usually has an option meaning "yes".

Built-in confirmation-skip options (examples)

Command Option to skip confirmation
apt -y / --yes
dnf / yum -y
cp / mv -f (force)
rm -f (no prompts)

Example: sudo apt install -y package-name

Lina: So the -y in apt install -y literally means "yes"!
Linny-senpai: Exactly. A command's own option targets just that command's prompt. yes, on the other hand, silences every confirmation indiscriminately. So when a dedicated option exists, use it instead, it's safer.

Rule of thumb

  • A dedicated -y / -f option exists → use it (safe, clear intent)
  • No dedicated option, or it asks repeatedly → consider yes | (after checking the target)

8. Mini Exercises: Try It Yourself

Conclusion: Three exercises on output, limiting the count, and custom strings. Don't forget Ctrl + C.

Lina: I want to learn by doing!
Linny-senpai: Great. You can stop any of these with Ctrl + C, so try them with confidence.

Exercise 1: Run yes with no arguments, confirm it prints y, then stop it with Ctrl + C.

Show hint

Type yes and press Enter. When the screen fills with y, press Ctrl + C.

Sample answer
$ yes

Stop it with Ctrl + C.

Exercise 2: Output the string OK exactly 4 times.

Show hint

Pipe yes STRING into head -n COUNT.

Sample answer
$ yes OK | head -n 4

Exercise 3: Install a package with apt while skipping the confirmation, without using the yes command.

Show hint

Use the command's own -y option.

Sample answer
$ sudo apt install -y package-name

9. Copy-Paste Templates

Conclusion: Keep the patterns for auto-answering, limiting count, empty lines, and safe alternatives handy.

Common patterns to keep around

# Auto-answer y to prompts (after checking the target)
yes | command

# Output the same line a fixed number of times (test data)
yes | head -n 5
yes test | head -n 3

# Repeat empty lines (Enter)
yes "" | command

# [Recommended] Skip confirmation with a dedicated option
sudo apt install -y package-name

# Stop yes
Ctrl + C

Next Reading