on OSX I do

sudo route delete default
sudo route add default

to change my default gateway.

On running a sh file with these I am asked for a password every time. Which I run as

sh changegw.sh.

How do I pass my password before hand?

  • Instead of passing your password to the script, you could invoke the script with sudo sh changegw.sh or run sudo bash to get a root shell, then run your script from there. Then you won't need sudo in the script. – rob Mar 17 '12 at 21:51

For sudo there is a -S option for accepting the password from standard input. Here is the man entry:

-S          The -S (stdin) option causes sudo to read the password from
            the standard input instead of the terminal device.

This will allow you to run a command like:

echo myPassword | sudo -S ls /tmp

While it is easiest to simply run the script as root, you could also create a group in the sudoers file that can run the program without a password, and add your username to that group.

I've never done this personally, though, and cannot recommend a specific line to add to your sudoers file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.