As
@gvisoc pointed out, the
apt
command accepts a
-y
parameter/switch, which will answer 'y' / 'yes' to any questions. So it's best to use that.
But for any other interactive commands that ask questions, but do not have a mechanism like the one
apt
uses, there is another way:
The
yes
command.
e.g.
Bash:
yes | someInteractiveCommand
Where
someInteractiveCommand
is a command which asks interactive yes/no questions.
So you could use:
Bash:
sudo apt update
yes | sudo apt upgrade
And the second line will use
yes
to repeatedly spam apt with 'y'. So every time apt asks a question, it will receive 'y'.
Note: Run a
sudo apt update
first. This will prompt you for your password and temporarily grant you root privileges.
Then run the yes command as shown above.
If you run the
yes
command before you’ve logged in via a previous
sudo
command, then
apt
will prompt for a password and
yes
will repeatedly send ‘y’ as the password and the command will fail.
However, with
apt
, it makes much more sense to use its built-in
-y
option. But if you have any other interactive CLI scripts/commands/programs, you could use
yes
.
A bit more on
yes
:
If you run the
yes
command on it's own, without any parameters, it will repeatedly spam 'y' to the screen as if you repeatedly pressed 'y' and the enter key.
e.g.
And it will keep going until you hit
ctrl
c
If you run it with parameters, it will just endlessly output the parameters to the screen
e.g.
will produce:
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Again, until you press
ctrl
c
to cancel.
Similarly, entering something like:
Bash:
yes Save the Cascadian tree octopus
you would get the following spammed to the screen:
Save the Cascadian tree octopus
Save the Cascadian tree octopus
Save the Cascadian tree octopus
Save the Cascadian tree octopus
Save the Cascadian tree octopus
Save the Cascadian tree octopus
... etc
By piping the output of the
yes
command to another interactive command/script,
yes
will continuously spam the script/command with 'y' or whatever other text you specified.
Any time the command prompts for input from the user, it will receive whatever text the
yes
command is sending.
If you want to answer 'no' to all questions output by a script, you could use:
yes no | someInteractiveCommand
Again, where
someInteractiveCommand
is any interactive command/script that asks yes/no questions. That would spam
someInteractiveCommand
with "no" until
someInteractiveCommand
ends. So every time it asks for input, it will receive 'no'.
Once again,
apt
has the
-y
option, which causes it to automatically answer 'y' to any interactive questions it has. But for other commands that ask for input, and
do not have a mechanism for automatically specifying an answer - you could just spam it with the
yes
command!
Also, before attempting to use
yes
with a command prefixed by
sudo
, you should log into
sudo
via another command immediately beforehand. Or consider switching to root using
su
. Then you could just use
yes | apt upgrade
.
Personally, on Debian - I completely disable the root account and do all system administration via sudo. But that’s just me!
yes
is a bit of a hidden gem. Not too many people know about it. I rarely use it for anything. But it comes in handy sometimes and is worth knowing for those rare times when you may want/need it!