When managing Linux environments, we often have to install a bunch of applications through some package manager. Usually, we write scripts to automate this process.
Without answers to these questions, our scripts can’t run. They would hang, waiting for user input.
There are different ways to auto-answer these questions with a yes.
Examples in this article were tested in Bash but should work in other POSIX compatible shells as well.
2. Checking for Auto-Yes
Most package managers like yum or apt-get have built-in options to auto-answer yes to all questions during the install.
For example, let’s take a closer look at yum by printing its help information:
$ yum --help ... -q, --quiet quiet operation -v, --verbose verbose operation -y, --assumeyes answer yes for all questions --assumeno answer no for all questions --version show Yum version and exit ...
As shown in this partial output, we can use -y to automatically answer yes to all questions. Now, we can install multiple packages:
yum install -y vim nginx postgresql
Or, maybe we’d like to update all packages in our system:
yum update -y
The apt-get command has a similar -y option. Unfortunately, this doesn’t apply to every tool out there.
3. The yes Command
When the program we’d like to execute has no option for auto-answering, we can use yes to our advantage. The yes command prints y (yes) repeatedly until killed:
$ yes y y y ^c
We can pipe this output to the command we execute. Suppose yum had no option like -y. In that case, we could have used yes to auto-answer all questions like this:
yes | yum install vim nginx postgresql
Now, every time the installation requires us to answer some question, yes will provide it with y, no matter if it’s a y/n question or not.
There is one more thing to yes. We might have to script an answer different than y, maybe yes or Y. To accommodate this, yes accepts a single argument, the string that will be printed repeatedly.
For example, to print YES, we’ll use:
$ yes YES YES YES YES ^c
We should note that using yes will answer y to all questions, not just the yes/no ones.
4. Echo as an Alternative
As an alternative to yes, we can get similar results with the echo command. The echo command actually works quite similar to yes in this context. It prints a given string argument to the standard output:
$ echo yes yes
We can pipe this output to any program the same way we did before:
echo y | yum install vim nginx postgresql
However, there’s a major caveat to this approach. Unlike yes, echo will only print one line of output. Therefore this approach won’t work when we have to answer multiple questions.
When scripting automated installs, we often have to be able to auto-answer one or more questions with yes. In this article, we reviewed some alternatives.
First, we saw some package managers like yum and apt-get have some built-in support for it.
Then, we learned that for commands that don’t have this built-in support, we could use the yes command to our advantage.
Finally, we saw we have to be careful applying a solution using echo as it will only be able to answer only one question.