yes

A lot of utilities, such as fsck, have a -y option to say “yes” to all questions that they may ask. The good ones are then written so that “yes” to any question means basically the same thing — whether it’s to mark superblocks clean or to remove inodes, the -y flag to fsck indicates that it should do whatever is necessary to fix the filesystem. This can be very useful for scripts as it means that they do not have to interact with the user. Some tools do not have this option, and yes can be useful when using these interactively. What yes does is spew out a continuous supply of the letter y. These two commands are equivalent:

# fsck -y /dev/sdf1
# yes | fsck /dev/sdf1
note.ai

As an alternative, yes can take any other text as a parameter, and it will display that instead.

One use for yes that may not look too obvious at first glance is this one-line command:

yes no | cp -i * /tmp

This pipes the word “no” into the cp command, which, when called as cp -i, will always query before overwriting a file. The net result of this is that existing files will not be replaced, but other files will be copied into /tmp. Not often required, but it is at least entertaining for the way it looks as well as being occasionally useful, too.

Get Shell Scripting: Expert Recipes for Linux, Bash, and More now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.