Insert a password into the terminal through a script?
By David Jones •
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.
21 Answer
You can use something like this in your script:
echo 'yourPassword' | sudo -S yourCommandThe -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 yourCommandHope it helps.