How to Automate Prompts Using the yes Command in Linux

When you are writing a complex bash script that installs a massive piece of software (like a database server or a web panel), the installation program will frequently pause and prompt the user to type “y” or “yes” to confirm the action before continuing. If you are trying to completely automate the server deployment so it runs entirely hands-free in the background, those manual confirmation prompts will cause the script to hang forever. To forcefully answer every single prompt automatically, you must use the yes command.

How the yes Command Works

The yes command is incredibly simple, but mathematically dangerous. If you simply type yes into an empty terminal and press Enter, the command will immediately lock the terminal into an infinite loop, violently printing the letter “y” down your screen thousands of times per second until you physically force the program to stop by pressing Ctrl + C.

yes

While an infinite loop of “y” seems useless on its own, it becomes an incredibly powerful automation tool when combined with the Linux pipe (|) operator.

Automating Installation Prompts

If you have an installation script called setup.sh that is known to ask the user “Are you sure you want to install this? (y/n)” three different times, you can pipe the infinite loop of the yes command directly into the script.

yes | ./setup.sh

As the setup script runs, the moment it pauses and waits for user input, the yes command instantly injects a “y” and presses Enter, allowing the script to continue without any human intervention. When the setup script finally finishes and closes, the pipe breaks, and the infinite loop of the yes command is safely terminated automatically.

Outputting Custom Strings

While the command is named yes, and it outputs the letter “y” by default, you can actually instruct it to infinitely output any custom string of text you desire. This is highly useful for generating massive amounts of dummy data to test the performance of a log-rotation script or fill up a hard drive partition.

To infinitely output a custom sentence, simply type the sentence after the command:

yes "Testing Log System" > dummy_data.txt

This command will instantly start writing millions of lines of the phrase “Testing Log System” into the dummy_data.txt file. Warning: The yes command is so fast it can generate gigabytes of text in a matter of seconds. Be fully prepared to press Ctrl + C immediately to kill the process before it fills up your entire hard drive.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.