Shell builtins
yes
Print a line (y by default) over and over until killed or the reader stops reading.
yes [STRING...]
yes writes y and a newline, forever. Given arguments, it repeats them instead, joined by
spaces. It stops when whatever reads its output closes the pipe, which is why yes | head -3
finishes instantly: head exits after three lines and yes is killed by SIGPIPE on its next
write.
Its original job is answering prompts: piping yes into a command that asks "are you sure?"
answers every question. Today most tools have a flag for that (apt-get -y, rm -f, cp -f),
which is better because it is explicit. The other job is generating repeated input quickly:
test files of a given size, or a fixed number of identical lines.
Examples
Print y repeatedly
yes | head -n 3
y y y
Without head this would scroll until you press Ctrl-C.
Repeat your own text
yes 'all good' | head -n 2
all good all good
Any arguments replace y. Several arguments are joined with spaces.
Answer every prompt of an interactive command
Setup run before this example
touch a.tmp b.tmp c.tmpyes | rm -i *.tmp
ls *.tmp
rm: remove regular empty file 'a.tmp'? rm: remove regular empty file 'b.tmp'? rm: remove regular empty file 'c.tmp'? ls: cannot access '*.tmp': No such file or directory
rm -i asks before each file; yes answers y to all three. The prompts are printed
without newlines because the answers come from the pipe, not the keyboard. The final ls
fails because the files are gone.
Answer no to every prompt
Setup run before this example
touch a.tmp b.tmpyes n | cp -i notes.txt a.tmp 2>&1; echo
cmp -s notes.txt a.tmp || echo "a.tmp was not overwritten"
cp: overwrite 'a.tmp'? a.tmp was not overwritten
yes n declines every question. cp -i asked before overwriting a.tmp, got n, and left
the file alone.
Make a file with N identical lines
yes 'GET /health 200' | head -n 1000 > requests.log
wc -l requests.log
sort requests.log | uniq -c
1000 requests.log 1000 GET /health 200
Handy for load-testing a parser or checking how a tool handles repetition.
Make a file of an exact size
yes 'lorem ipsum' | head -c 1M > big.txt
ls -l big.txt
-rw-r--r-- 1 dev dev 1048576 Oct 31 20:22 big.txt
head -c 1M cuts the stream at exactly 1,048,576 bytes, mid-line if necessary. This is a
quick way to make compressible test data; use /dev/urandom for incompressible data.
Generate empty lines
yes '' | head -n 3 | cat -A
$ $ $
An empty string argument makes yes print bare newlines. cat -A marks each line end
with $ so the empty lines are visible.
Measure how fast it writes
yes | head -c 500M | wc -c
524288000
yes writes large buffers rather than one line at a time, so 500 MiB passes through the
pipe in well under a second. This makes it a cheap way to test pipe throughput.
Common pitfalls
Running
yeswithout something to stop it. In a terminal it floods the screen until Ctrl-C; redirected to a file it fills the disk. Always pipe it into a reader that stops, such ashead.Using
yes | commandwhere the command has an assume-yes flag.apt-get -y,rm -fandfsck -yare explicit and do not depend on how the prompt reads its input.Expecting
yes |to answer password prompts. Programs such assshandsudoread passwords from the terminal device, not from standard input.
GNU vs BSD and macOS
The examples above ran the GNU/Linux version. These are the differences that matter on macOS and the BSDs (written from their documentation, not run here).
BSD and macOS
yesrepeat only the first argument; GNU joins all arguments with spaces. Quote the text as one argument to get the same result everywhere.
Options worth knowing
A selection, described in our own words. For the full list, see yes(1) or yes --help.
| Option | What it does |
|---|---|
--help | Print usage and exit. |
--version | Print the coreutils version and exit. |
Related
headPrint the first lines (or bytes) of files; 10 lines by default.seqPrint a sequence of numbers, with a chosen start, step, separator and format.printfPrint text and values through a format string, with precise control over spacing, numbers and escapes.trueDo nothing and exit with status 0 (success).- Pipes and pipelinesHow cmd1 | cmd2 connects processes, which exit status a pipeline returns, PIPESTATUS, pipefail, SIGPIPE, and why variables set in a piped loop vanish.