yes

shellLinux/Unix
The yes command is one of the most frequently used commands in Linux/Unix-like operating systems. yes Output a string repeatedly until killed

Quick Reference

Command Name:

yes

Category:

shell

Platform:

Linux/Unix

Basic Usage:

yes [options] [arguments]

Common Use Cases

    Syntax

    yes [STRING]...
    yes [OPTION]

    Options

    Option Description
    --help Display help information and exit
    --version Output version information and exit

    If no STRING is specified, 'y' is used as the default output string.

    Examples

    How to Use These Examples

    The examples below show common ways to use the yes command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    # Output "y" repeatedly until terminated (Ctrl+C) yes
    # Output "hello" repeatedly yes hello
    # Output "ok sure" repeatedly yes "ok sure"
    # Advanced Examples Advanced
    # Use yes to automatically answer prompts in scripts yes | rm -i *.txt
    # Use yes with multiple arguments yes hello world # Output a custom string 10 times and then stop yes "custom response" | head -n 10 # Create a large test file quickly yes "test data" | head -n 1000 > testfile.txt # Keep a connection alive with continuous data yes | nc example.com 80

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The `yes` command is a simple yet useful utility in Unix/Linux systems that outputs a string repeatedly until it is terminated. While seemingly basic, it serves several practical purposes in shell scripting and system administration. **Core Functionality:** 1. **Default Behavior**: When run without arguments, `yes` outputs the letter 'y' followed by a newline character continuously until it receives a termination signal (typically Ctrl+C). 2. **Custom Output**: When provided with an argument, `yes` will output that string repeatedly instead of the default 'y'. 3. **Multiple Arguments**: If multiple arguments are provided, `yes` will output all of them as a single space-separated string. **Common Use Cases:** 1. **Automatic Confirmation**: The primary use of `yes` is to provide automatic confirmation to commands that require interactive input. For example, `yes | rm -i *.txt` will automatically answer 'y' to all confirmation prompts from the `rm` command. 2. **Stress Testing**: `yes` can be used to generate a constant stream of output for testing purposes, such as filling up disk space, testing I/O performance, or stressing the CPU. 3. **Keep-Alive Signals**: In some scenarios, `yes` can be used to keep a connection or process alive by continuously sending data. 4. **Generating Test Data**: Combined with other commands, `yes` can quickly generate large volumes of test data for various purposes. **Performance Characteristics:** 1. **CPU Usage**: The `yes` command is optimized to produce output very efficiently. It buffers its output to minimize system calls, making it surprisingly efficient at generating large volumes of data. 2. **System Impact**: Despite its efficiency, continuous use of `yes` can consume significant CPU resources and generate high I/O loads if not properly redirected or limited. **Implementation Details:** 1. **Buffer Optimization**: The GNU implementation of `yes` uses large buffers to minimize the number of system calls needed to produce its output, making it more efficient than a simple loop that prints a string. 2. **Memory Footprint**: `yes` has a minimal memory footprint, making it suitable for use even on resource-constrained systems. **Practical Examples:** 1. **Unattended Installations**: `yes` can be used to automate installation scripts that require confirmation, such as `yes | apt-get install package`. 2. **Creating Test Files**: To create a large file quickly for testing: `yes "test data" | head -n 1000000 > large_test_file.txt`. 3. **Stress Testing Disk I/O**: `yes > /dev/null` will generate continuous output that is discarded, stressing the CPU but not the disk, while `yes > /tmp/test_file` will stress both CPU and disk I/O. 4. **Simulating User Input**: In automated testing scripts, `yes` can simulate user input for interactive programs. **Limitations and Considerations:** 1. **Termination**: Since `yes` runs indefinitely by default, it must be explicitly terminated (usually with Ctrl+C) or limited using pipes to commands like `head`. 2. **Resource Consumption**: If output is not properly redirected or limited, `yes` can quickly fill up available disk space or consume significant system resources. 3. **Buffering Issues**: When piping `yes` to other commands, be aware of potential buffering issues that might affect the expected behavior. **Historical Context:** The `yes` command is part of the GNU Coreutils package and has been a standard utility in Unix-like systems for decades. Its simplicity belies its utility in automation and system administration tasks, particularly in the era before more sophisticated automation tools were widely available. **Similar Commands:** 1. **echo**: For single-instance output rather than continuous repetition. 2. **printf**: For formatted output, but again without the continuous repetition feature of `yes`. 3. **seq**: For generating sequences of numbers, which can serve some similar testing purposes. 4. **while true**: A shell construct that can replicate `yes` functionality with `while true; do echo "string"; done`. In summary, `yes` is a simple but powerful command that excels at providing automatic responses to interactive prompts and generating continuous output for testing and other purposes. Its efficiency and minimal resource requirements make it a valuable tool in the Unix/Linux command-line arsenal.

    Related Commands

    These commands are frequently used alongside yes or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the yes command works in different scenarios.

    $ yes
    View All Commands