How to Automatically Answer Yes or No in Linux-Unix Commands

A lot of the time in Linux / Unix commands or inside a script we have to provide a confirmation input like Yes or No. This manual entry position can be avoided. In this article, we will discuss how we can do an auto-answer Yes or No for Linux-Unix commands to make life easier for sysadmins.

How to Automatically Answer Yes or No in Linux-Unix Commands

Method 1: Repeat yes or no

You could use this method by echoing yes or no before the actual command or script and placing the pipe “|” between these two teams. Let’s show an example to develop this technique.

Let’s say you have five text files in the / tmp / AndreyEx directory as shown below.

[[email protected] AndreyEx]# ll
total 0
-rw-r--r--. 1 root root 0 Jun 26 11:56 1.txt
-rw-r--r--. 1 root root 0 Jun 26 11:56 2.txt
-rw-r--r--. 1 root root 0 Jun 26 11:56 3.txt
-rw-r--r--. 1 root root 0 Jun 26 11:56 4.txt
-rw-r--r--. 1 root root 0 Jun 26 11:56 5.txt
[[email protected] AndreyEx]# pwd
/tmp/AndreyEx
[[email protected] AndreyEx]#

Now you want to delete all or only one file “1.txt” in this directory. The answer is simple, the rm command. However, this command will ask yes or no before proceeding to delete the file. As shown below:

[[email protected] AndreyEx]# rm 1.txt
rm: remove regular empty file ‘1.txt’? y

In the above method, we provided the “y” input for the rm command. To avoid this, we will prepend the rm command, as shown below:

[[email protected] AndreyEx]# echo y|rm 1.txt
rm: remove regular empty file ‘1.txt’? [[email protected] AndreyEx]# ll
total 0
-rw-r--r--. 1 root root 0 Jun 26 11:59 2.txt
-rw-r--r--. 1 root root 0 Jun 26 11:59 3.txt
-rw-r--r--. 1 root root 0 Jun 26 11:59 4.txt
-rw-r--r--. 1 root root 0 Jun 26 11:59 5.txt
[[email protected] AndreyEx]#

We have made an automatic response to the command, but there is still a problem. Team echo cannot be used to answer as a continuous yes or no to commands. In this situation, you need to use the following method.

Method 2: command yes.

Yes command is another option if you need to automatically answer Yes or No to commands on Linux-Unix.

Basically what the yes command does is it will continuously output the lines passed to them or even pass the yes string alone. Hence, we can use this feature to automatically answer yes or no to Linux / Unix commands / scripts. Let’s take an example. We have five files as before, and we want to delete all of these files without answering a Yes or No confirmation question. Then try the method below which uses the command “Yes“.

[[email protected] AndreyEx]# ll
total 0
-rw-r--r--. 1 root root 0 Jun 26 11:58 1.txt
-rw-r--r--. 1 root root 0 Jun 26 11:58 2.txt
-rw-r--r--. 1 root root 0 Jun 26 11:58 3.txt
-rw-r--r--. 1 root root 0 Jun 26 11:58 4.txt
-rw-r--r--. 1 root root 0 Jun 26 11:58 5.txt
[[email protected] AndreyEx]# yes |rm *
rm: remove regular empty file ‘1.txt’? rm: remove regular empty file ‘2.txt’? rm: remove regular empty file ‘3.txt’? rm: remove regular empty file ‘4.txt’? rm: remove regular empty file ‘5.txt’?
[[email protected] AndreyEx]# ll
total 0
[[email protected] AndreyEx]#

Here’s what happened when the yes command is given a continuous response, like Yes for the next rm command. The result of deleting all files without manual intervention. This cannot be achieved with the echo-yes method as it gives input as yes only once !!!

Method 3: command yes with a different input.

Let’s say you don’t want answers instead of Yes. This time when you want to answer no for all confirmations given by the command. An example follows. Here we answered no to all of the answers. Hence, no files were deleted by the system.

[[email protected] AndreyEx]# yes n |rm *
rm: remove regular empty file ‘1.txt’? rm: remove regular empty file ‘2.txt’? rm: remove regular empty file ‘3.txt’? rm: remove regular empty file ‘4.txt’? rm: remove regular empty file ‘5.txt’?
[[email protected] AndreyEx]# ll
total 0
-rw-r--r--. 1 root root 0 Jun 26 11:59 1.txt
-rw-r--r--. 1 root root 0 Jun 26 11:59 2.txt
-rw-r--r--. 1 root root 0 Jun 26 11:59 3.txt
-rw-r--r--. 1 root root 0 Jun 26 11:59 4.txt
-rw-r--r--. 1 root root 0 Jun 26 11:59 5.txt
[[email protected] AndreyEx]#

Even you can use the following command instead of the above.

# yes no |rm *

Conclusion:

Thus, we can use the command yes to automatically answer Yes or No for Linux-Unix commands. This greatly helps the sysadmin to execute commands in a continuous manner, which makes their life easy and comfortable.

Sidebar