4

How to pipe a Y or yes to a program while invoking with sudo?

We can type like this

yes | command
yes | yum update

How to pipe the y from yes into a program via sudo like the following?

yes| sudo command

The y from yes is be passed into command and should go into sudo and sudo shall ask for password normally. How can I do this?

12

sudo reads the password from the terminal directly, not from its standard input, unless the -S option is used. Thus

yes | sudo command

should prompt for the password (if necessary), without reading from yes, then run command as root with its standard input fed from yes’s standard output.

If that doesn’t work, you can run the whole pipeline under sudo using something like

sudo sh -c "yes | command"
  • yes | sudo command not passing y to command although sudo is asking for password. sudo sh -c "yes | command" is working fine. But is there a way to do the same without invoking another child shell explicitly? – Abhik Bose Dec 15 '17 at 13:50
  • 1
    If your pipe doesn’t survive sudo (yes | sudo command), then you need something else to set a pipe up for you; sudo itself can’t do that, so you need a subshell to do it for you (sudo sh -c "yes | command"). – Stephen Kitt Dec 15 '17 at 14:18
  • 1
    Is there more than one sudo implementation? – Stéphane Chazelas Dec 15 '17 at 15:59
  • @Stéphane good point, there probably is just sudo.ws. – Stephen Kitt Dec 15 '17 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.