Use yes Safely for Repeatable Shell Input

yes prints a line forever, which makes it perfect for scripted testing and a quiet trap if you forget to bound it. You will print a repeated line, combine words into one, and feed a deliberately bounded stream into another command.

Allow about five minutes. You need a Linux shell with GNU coreutils installed. The examples below were checked with GNU coreutils 9.4. No elevated privileges are required.

1. Check the installed command

First confirm which executable your shell will run and record its version:

$ command -v yes
/usr/bin/yes
$ yes --version | head -n 1
yes (GNU coreutils) 9.4

The local manual describes yes as outputting a string repeatedly until it is killed. It takes either one or more strings, or a single option; with no string at all, its default line is y. The version matters when documenting a result, since another implementation may handle options differently.

Checkpoint: If command -v yes returns nothing, repair the coreutils package through your normal package manager. Do not copy an executable into /usr/bin by hand.

2. Print a bounded sample

Running yes alone is unbounded by design: it keeps writing until standard output closes or a signal arrives. Cap it with head for any demonstration or test:

$ yes | head -n 3
y
y
y

head reads three lines and exits, which closes the pipe; yes then stops the moment its next write hits that closed pipe. It is a clean way to demonstrate the command without having to find and kill a process afterwards.

The limit belongs to the consumer, not to yes itself. Forget head and a terminal fills fast, or a redirected file grows until the disk is full.

3. Repeat your own line

Pass text after the command name. Every argument you give lands in one output line, separated by a single space:

$ yes "test input" | head -n 3
test input
test input
test input
$ yes one two | head -n 2
one two
one two

Quoting controls how your shell builds the arguments before yes ever sees them: yes "test input" is one argument containing a space, while unquoted yes test input is two arguments that still print as test input per line. Quoting is still the clearer habit when the text is meant to be a single value, and shell expansion happens before yes runs, so check unusual input before putting it in an automated command.

4. Feed a command without an endless stream

A bounded stream is handy for testing a prompt or a repeated-answer path. This gives a command exactly five lines:

$ yes n | head -n 5 | while IFS= read -r answer; do
>     printf 'answer=%s\n' "$answer"
> done
answer=n
answer=n
answer=n
answer=n
answer=n

The loop here is only a demonstration consumer. Swap in the real command you are testing, but keep head -n 5 while you are still working out how much input it actually consumes. A consumer that exits early can send yes a broken-pipe signal; that is normal for this pipeline, not proof that five lines went missing.

Warning: do not point an unbounded yes stream at a real confirmation prompt unless you have checked every question it might ask. A program can ask something different after an error, and a repeated answer can wave through a destructive action.

5. Avoid the redirection trap

Standard output can be redirected, but shell redirection with > truncates the destination before yes even starts:

$ yes y > answers.txt

Warning: this creates an unbounded file and can destroy an existing answers.txt in the process. Prefer a bounded command and a fresh destination when testing output:

$ yes y | head -n 5 > answers-test.txt
$ wc -l answers-test.txt
5 answers-test.txt
$ cat answers-test.txt
y
y
y
y
y

Recovery: if you start an unbounded foreground command by accident, press Ctrl+C. If you have already redirected into a valuable file, stop the command first and do not assume recovery is possible: restore from a backup or snapshot, because a shell cannot undo a truncation after it happened.

6. Use the options that exist

The installed manual documents --help and --version for local discovery:

$ yes --help
Usage: yes [STRING]... or: yes OPTION
Repeatedly output a line with all specified STRING(s), or 'y'.
$ yes --version | head -n 1
yes (GNU coreutils) 9.4

Do not go looking for a count option: yes does not have one in this installed manual. Counting belongs to a separate command such as head -n, which also keeps the boundary visible in the pipeline.

Done means