echo y| del *.* but for Linux :)

abmvk

Member
Joined
Mar 3, 2024
Messages
39
Reaction score
23
Credits
345
This probably is a simple question, but I would like to know if this works before I try. I have very simple script to update (Debian, apt and Rust toolchain) my system using a touchscreen. But when apt wants to update I have to agree with a "Y" and a password.

Now I remember from ms-dos times you could use piping like this: echo Y|del . (not a very safe way, but it worked). Could I do something like that in Linux too? so that would be something like: echo Y|sudo apt upgrade?

Also, can I in the same way skip the asking for a password?
 


You can type sudo apt -y upgrade to not have to say "Y".

To have sudo not asking your password, duck duck go "passwordless sudo" but be very careful. I don't think it'd be very safe to apply system-wide. If you just want to apply it to apt commands, check this source to see if this fits you https://linuxhandbook.com/sudo-without-password/
 
Great, thanks!

Tried it and works like a charm.
 
Last edited:
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 updatefirst. 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.
y
y
y
y
y
y
y
y
... etc
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.
Bash:
yes Yes
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!
 
Last edited:
I generally use root to do system management work so I don't have to use sudo all the time.

Signed,

Matthew Campbell
 
I love that yes command :)

I never use root, so sudo it is for me!
 
I only use SUDO to

sudo su -

but that is because this is setup this way but not by me. This avoids need of the ROOT password, and still "login as ROOT", but it can be asked if that is even a good thing. I note that this way I have way too much power over the system, and that while not being the system administrator because the system administrator knows the Root pass and has setup this SUDO in the first place. Logically, he is not using SUDO probably.
 


Top