4

I was wondering if there was a way to insert a password when it's asked for in the terminal while using a script. I would prefer to not have to type it but I suppose that's not the worst thing that could happen.

2
  • This expect question might help you. Jan 20, 2018 at 8:55
  • This also depends a bit on the commands you want to run in the script. If you want no password at all for a certain script have a look at visudo
    – derHugo
    Jan 20, 2018 at 11:17

1 Answer 1

4

You can use something like this in your script:

echo 'yourPassword' | sudo -S yourCommand

The -S flag makes sudo read the password from the standard input. You can check it in manual pages using man sudo:

-S, --stdin

Write the prompt to the standard error and read the password from the standard input instead of using the terminal device. The password must be followed by a newline character.

If you get an error using this, it's because your sudo access token is active, to get around that, you could use -k to reset the access token:

echo 'yourPassword' | sudo -kS yourCommand

Hope it helps.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .