title: Downloading a File from SSH with Sudo
body: <p>I am trying to retrieve a file <code>file.txt</code> from a remote server accessible from <code>ssh</code>, and copy its content to a file on local. But I want this process to be done in a <code>.sh</code> file.</p>
  <p>The problem arises from the fact that <code>file.txt</code> is only accessible from the root, meaning that in order to manually see the contents of the file I have to run:<code>sudo cat file.txt</code>, and insert my password through terminal. So commands such as <code>scp</code> will not do the job as they throw a <code>Permission Denied</code> error.</p>
  <p>To bypass this, I have done the following.</p>
  <pre><code>ssh -t user@remote 'sudo cat file.txt'
  </code></pre>
  <p>However, in the bash file I intend to save the result in a variable as follows:</p>
  <pre><code>CONTENT=$(ssh -t user@remote 'sudo cat file.txt')
  </code></pre>
  <p>Which stores the content of the file in the variable <code>CONTENT</code> after asking for my password.</p>
  <p>This works, if the user types in their password and press enter the variable gets populated with the contents of <code>file.txt</code>; but the password interface is no longer printed in the terminal. This means that whoever runs this bash file does not see the line:</p>
  <pre><code>[sudo] password for user:
  </code></pre>
  <p>or the line below when the password entered is incorrect.</p>
  <pre><code>Sorry, try again.
  [sudo] password for user:
  </code></pre>
  <p>To make matters worse, sometimes the first few characters of the password actually get typed into the terminal!</p>
  <p>Is there a way to work around this? Is there a way for the user to actually see the password interface?</p>
  <p>Interestingly, the lines <code>[sudo] password for user:</code> and <code>Sorry, try again.</code> can be seen in the variable <code>CONTENT</code>, so not only the password interface disappears, but it also pollutes the variable <code>CONTENT</code>. This is not an issue however as we can simply parse <code>CONTENT</code>.</p>