In the world of Linux, there are countless command-line utilities and tools that can help you perform a wide range of tasks efficiently.
One such tool is the yes command, which may seem simple at first glance but can be quite useful in various scenarios.
Let us explore the yes command and how to use it effectively. Linux Mint is used for this article but you can try it with any other Linux distro too.
What is the yes Command
The yes command is a simple command that endlessly prints the string y (or any other specified string) to standard output.
To stop, press Ctrl+C.
Its primary purpose is to automate the process of confirming yes/no prompts in scripts and command-line operations.
So, it becomes a useful tool for automating tasks like running unattended scripts and installation.
You can also repeatedly print custom strings with the yes command.
The syntax is:
So, to repeatedly print ihaveapc.com, the command will be:
Press Ctrl-C to exit the command as before.
The yes command for tasks
One of the most common uses of the yes command is for automating tasks that require user input.
For example, when deleting multiple files using the rm command, you might encounter multiple prompts asking for confirmation.
The yes command can automatically confirm these prompts for you without manual intervention.
For this, pipe the output of the yes command to rm as follows:
yes | rm -i *.*
So, in this example, the above command will automatically delete all the files with y as the automated input for delete confirmation about each of these files that are to be deleted.
The yes command for installing and upgrading packages
When using package managers for various Linux distros, you might need to confirm the installation of packages by typing y or yes. You can streamline this process using the yes command.
For example, the apt upgrade command for Debian-based distros requires you to type y or Y to continue.
You can automate this by piping the yes command as follows:
yes | sudo apt upgrade
The upgrade command will now automatically take the default y output from yes command and proceed with the upgrading of packages.
The yes command for interactive shell scripts
Interactive shell scripts that require a user input can also be automated with the yes command.
Example: Consider a shell script named details.sh which requires the user to type a specific word – Continue to proceed with its execution.
The yes command for this can be modified as follows:
yes Continue | ./details.sh
This will pipe the output of the yes command, which is the word Continue to the script and thereby gets the script to run without the user intervention.
Note that running the yes command in this example without the specific string (Continue) will cause the script to exit as the script logic explicitly requires the word Continue and nothing other than that.
So piping only the default yes command will terminate this script in this case.
This shows that you can modify the yes command with the custom string output as required by the scripts and tasks that you want to run unattended.
Accessing the man page
As with all Linux commands, you can quickly open the man page for yes command:
It shows you the purpose of this command, its syntax and other useful information.
Overall, this is a pretty useful and overlooked Linux command. The yes command might appear simple, but it plays a valuable role in automating tasks and unattended script execution on a Linux system.
Whether you need to confirm actions, install packages, or provide custom responses, it is a versatile tool that can save you time and effort.
However, use it with caution, as automatic confirmation of prompts can lead to unintended consequences if misused.