title: Pass password to sudo for command that expects input
body: <p>I am trying to create a Shell script and (to avoid typing, furthermore ignoring security related issues for now) want to directly pass the password to the "sudo" command, e.g.,</p>
  
  <pre><code>pword="mypassword"
  echo $pword | sudo -S whoami
  </code></pre>
  
  <p>This works just fine. But now when the command itself expects an input, this method seems to fail, e.g.,</p>
  
  <pre><code>echo $pword | sudo -S cat&lt;&lt;&lt;"Hello"
  </code></pre>
  
  <p>This would lead to an 'incorrect password' error. Currently, my solution is to run a "dummy command" like in example one first, and use the fact that for the second command the system does not prompt for a password again. However, does anyone know a better solution to get it to work? </p>