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.
Asked
Active
Viewed 2.8k times
4
-
[This expect question](https://stackoverflow.com/q/4780893/9224859) might help you. – Robert Riedl Jan 20 '18 at 08: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](https://askubuntu.com/a/192062/423684) – derHugo Jan 20 '18 at 11:17
1 Answers
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.
galoget
- 2,943
- 2
- 20
- 24