Bash Wiki
Posts (Latest 30 updated) :
Read all
Contents:
  1. [Linux] Bash yes Usage equivalent in English: Generate repeated output
    1. Overview
    2. Usage
    3. Common Options
    4. Common Examples
    5. Tips

[Linux] Bash yes Usage equivalent in English: Generate repeated output

Overview

The yes command in Bash is a simple utility that outputs a string repeatedly until it is interrupted. By default, it outputs the string “y” followed by a newline, which is often used to automate responses to prompts in scripts or command-line operations.

Usage

The basic syntax of the yes command is as follows:

yes [options] [arguments]

Common Options

  • -h, --help: Display help information about the command and its options.
  • -V, --version: Show the version of the yes command.
  • STRING: If provided, yes will output this string instead of the default “y”.

Common Examples

  1. Default Usage: Output “y” repeatedly.
    yes
    
  2. Custom String: Output a custom string repeatedly.
    yes "I agree"
    
  3. Redirecting Output: Use yes to feed input into another command.
    yes | rm -i file.txt
    

    This will automatically respond “y” to the prompt asking for confirmation to delete file.txt.

  4. Limiting Output: To limit the number of lines outputted, you can use head.
    yes | head -n 5
    

    This will output “y” five times.

  5. Using with xargs: Combine yes with xargs to execute a command multiple times.
    yes | xargs -n 1 echo
    

    This will echo “y” repeatedly, one per line.

Tips

  • Use yes in scripts to automate responses to commands that require user input.
  • Be cautious when using yes with destructive commands (like rm), as it can lead to unintended data loss if not controlled properly.
  • Combine yes with other commands using pipes to create powerful command-line workflows.