The yes command in Linux: generate automatic repetitive output

Introduction

In the world of system administration and scripting, it is often necessary to generate a steady stream of text to feed other commands, test applications, or quickly fill files. The Linux yes command does exactly that: it produces an unlimited output of a specified string (by default, the letter «y») until it is interrupted.

What is the yes command?

Originating from early Unix systems, yes is a simple utility that belongs to the coreutils package. Its sole purpose is to repeatedly write a string to standard output, followed by a newline, until it receives a termination signal (such as Ctrl+C) or its output is redirected to another process that closes it.

Basic syntax

The simplest form is:

yes [string]

If [string] is omitted, the command assumes «y» and produces an endless column of y followed by \n. For example:

yes

Will produce:

y
y
y

Practical examples

  • Automatically respond to prompts: Many programs ask if you want to continue (y/n). Redirecting the output of yes to their standard input allows automating the response:

    yes | apt-get update

    This assumes that all questions will be answered with «y».

  • Generate test files: If you need a large file filled with a pattern, combine yes with head or dd:

    yes 'Linux' | head -n 10000 > prueba.txt

    You will get 10 000 lines with the word Linux.

  • Test server resilience: In load testing, you can use yes to produce simple data traffic:

    yes | nc -v example.com 80

    This will send a continuous stream of «y» to port 80, useful for checking how the server handles persistent connections.

Useful options

Although yes is minimalist, GNU coreutils includes some options that can be useful:

  • --help: displays the standard help.
  • --version: displays the command version.

There are no options to change the delimiter or suppress the newline; if you need output without a newline, you can combine it with tr -d '\n' or use printf in a loop.

Precautions and best practices

  • Avoid infinite loops in production: Letting yes run uncontrolled can consume CPU and fill disks if its output is redirected to a file. Always limit the amount with head, sed or a timeout.
  • Combine it with timeout: To ensure the command does not run indefinitely, use:

    timeout 30s yes 'prueba' > salida.txt

    This will stop yes after 30 seconds.

  • Be careful with interactive prompts: Always forcing «y» can be dangerous if the program expects a different response to abort destructive operations. Check the command’s documentation before automating responses.

Conclusion

The yes command may seem trivial, but its ability to generate repetitive and endless text streams makes it a valuable tool for administrators, developers, and testers. From automating responses during installations to creating massive test files, understanding how it works and its limitations will allow you to use yes safely and efficiently in any Linux environment.

This post is also available in ESPAÑOL.

Leave a Reply

Your email address will not be published. Required fields are marked *

Esta obra está bajo una Licencia Creative Commons Atribución 4.0 Internacional para Francesc Roig francesc@vivaldi.net .