Doing this kind of stuff is not safe or standard practice (in fact many consider it disasterous), it is really not a good idea to put a password in a script. A more standard approach would be simply to expect the whole script to be executed with root privileges, or just to have the script prompt for a password. You can also allow various commands to be run via sudo without a password by particular users by using the NOPASSWD
option in /etc/suoders
.
However, now that you are aware of the risks, it is possible to use sudo -kS
to have sudo read the password from stdin
:
sudo -kSs << EOF
password
whoami
echo "Not a good idea to have a password encoded in plain text"
EOF