There's a few ways to do this. Here's two (pick the method you like):
Method 1: Using "askpass".
With this you always do sudo -A command. The -A argument tells sudo to execute a command that echos the password to stdout. That command is something you write. For this explaination let's call the command pw and stick it /usr/local/bin. So it's full pathname would be /usr/local/bin/pw.
sudo -A can get the pathname to pw a number of ways.
1. From the sudoers file.
Use visudo to add the following line to the sudoers file:
Defaults:ALL askpass=/usr/local/bin/pw
2. Using the SUDO_ASKPASS environment variable.
export SUDO_ASKPASS=/usr/local/bin/pw
This might work too (assuming SUDO_ASKPASS has been previously exported):
SUDO_ASKPASS=/usr/local/bin/pw sudo -A command
------------------------------------------------
Method 2: Have sudo read the password from stdin
echo -n password | sudo -S command
The -S option tells sudo to read the password from stdin so echo pipes it in (without the ending newline).
The only relatively secure scheme of these two methods is the askpass (-A) method. At least with that method you have a chance of encrypting/hiding your password down in the command that echoes it to stdout. The -S method would contain your password explicitly in a script somewhere unless you make other provisions to encrypt/hide it with that technique.