The yes command in Linux: generate automatic repetitive output

Introduction

In the world of system management and scripts programming, it is often necessary to generate a constant text sequence to power other commands, test applications or fill files quickly. The commandyesLinux performs this function precisely: it produces an unlimited output of a specified chain (by default, the letter «and») until it is interrupted.

What is command yes?

Originally from the first Unix systems,yesis a simple utility that belongs to the choreutils package. Its only purpose is to repeatedly write a chain on the standard output, followed by a line jump, until it receives a termination signal (asCtrl + C) or its departure is redirected to another process that closes it.

Basic syntax

The simplest way is:

yes [cadena]

If omitted[cadena], command assumes «and» and produces an endless column ofyfollowed by\n. For example:

yes

It shall generate:

y
y
y

Practical examples

  • Automatically respond to prompts: Many programs ask if you want to continue (y/n). Redirect the output ofyesto your standard input allows to automate the response:

    yes | apt-get update

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

  • Generate test files: If you need a large file full of a pattern, combineyeswithheadordd:

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

    You'll get 10,000 lines with the word Linux.

  • Test the resistance of a server: In load tests, you can useyesto produce simple data traffic:

    yes | nc -v example.com 80

    This will send a continuous flow of «and» to port 80, useful to verify how the server handles persistent connections.

Useful options

Althoughyesis minimalist, GNU choreutils includes some options that may be useful:

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

There are no options for changing the delimiter or suppressing the line jump; if you need a newline output, you can combine it withtr -d '\n'or useprintfin a loop.

Cautions and best practices

  • Avoid infinite loops in production: Letyesrunning without control can consume CPU and fill disks if your output is redirected to a file. It always limits the amount withhead, sedor atimeout.
  • Combine withtimeout: To ensure that the command does not run indefinitely, use:

    timeout 30s yes 'prueba' > salida.txt

    This will stop.yesafter 30 seconds.

  • Be careful with interactive prompts: Forging always «and» can be dangerous if the program expects a different response to abort destructive operations. Check the command documentation before automating answers.

Conclusion

The commandyesmay seem trivial, but its ability to generate repetitive and endless text flows makes it a valuable tool for administrators, developers and testers. From automating system responses to creating massive test files, understand its operation and limitations will allow you to useyesin a safe and efficient way in any Linux environment.

This work is under aCreative Commons License Attribution 4.0 International for Francesc Roig francesc @ vivaldi.net.

EnglishenEnglishEnglish