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
-yoption
Quick Summary
- Want to answer y to everything →
yes | command - But prefer a command's own
-yoption when it has one (safer) - Stop yes with Ctrl + C
1. What Is the yes Command?
Conclusion: yes simply prints the letter
yover and over, forever, until you stop it.
yes. What does it do? Does it say "yes" for you?y over and over, endlessly, until you stop it.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)".
y forever?|).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 | commandfeeds an automaticyto 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
yes | at the front answers every prompt for me!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
5. Limit the Count: yes | head
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
head at "stop here".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.
yes | seems like a magic fix. Why not use it for everything?Especially dangerous combinations
yes | rm -ri directory/— deletes the contents with no confirmationyes | command(overwrite / format tools) — overwrites important data with no confirmation
The prompt exists because the confirmation matters. Before silencing it with yes, always check exactly what the command targets (which files, which directory).
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/--yesoption. 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
-y in apt install -y literally means "yes"!Rule of thumb
- A dedicated
-y/-foption 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.
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