December 24, 2024

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.

 the yes command repeatedly prints the letter y by default

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:


yes string

So, to repeatedly print ihaveapc.com, the command will be:


yes ihaveapc.com
repeatedly print a custom string with the yes command

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. 

delete files while requiring a user prompt 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 *.*
adding yes command for deleting files without user prompt

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.

upgrading packages in Linux may need user confirmation

You can automate this by piping the yes command as follows:


yes | sudo apt upgrade
yes command for unattended installation and upgrading of packages

The upgrade command will now automatically take the default y output from yes command and proceed with the upgrading of packages. 

Pretty cool.

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. 

running a shell script that require user input

The yes command for this can be modified as follows:


yes Continue | ./details.sh
piping yes command with shell scripts for automated execution

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.

scripts may exit without the custom string provided through yes command

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:


man yes
man page for yes command

It shows you the purpose of this command, its syntax and other useful information.

Conclusion

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. 

All done.

By admin

Related Post