9

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?

Abhik Bose
  • 2,090
  • 2
  • 15
  • 30

1 Answers1

17

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"
Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164