Hello -- I'm writing a bash script that needs to sudo multiple commands. I can do this:
( whoami ; whoami )
but I can't do this:
sudo ( whoami ; whoami )
How do I solve this?
Hello -- I'm writing a bash script that needs to sudo multiple commands. I can do this:
( whoami ; whoami )
but I can't do this:
sudo ( whoami ; whoami )
How do I solve this?
The Brackets means that execute the command in a new bash.It execute the command with the interval of semicolon.Just use the code below instead.
(sudo whoami;sudo whoami)
BYW:the space is not necessary when using '()'.
You can also pipe the commands into sudo'ed bash:
sudo bash <<EOF
whoami
id
EOF
sudo only asks for your passwd the first time.The passwd answered is valid for about 5 minutes by default.You can change this value as this told.So just worry about the passwd prompt at the beginning of your script,then you can use sudo through out. changing Defaults:user_name timestamp_timeout's value to -1 may be a security hole on your system.