title: Create a file as one user and edit it as another user
body: <p>Im writing a shell to automate a process this shell will be run as root or as another user but not as the postgres user (meaning the user will just run the script) </p>
  
  <p>What i did as a postgres user while testing was</p>
  
  <pre><code>touch /var/lib/postgresql/10/main/recovery.conf
  sudo nano recovery.conf (wrote some content )
  ctrl +O
  </code></pre>
  
  <p>However whenever I try to do this by using the following lines in my shell as as another user (including root)</p>
  
  <pre><code>sudo -H -u postgres bash -c "touch /var/lib/postgresql/10/main/recovery.conf"
  </code></pre>
  
  <p>The file is created as postgres user which is what i intended, and then i run </p>
  
  <pre><code>sudo -H -u postgres bash -c echo "content" &gt; /var/lib/postgresql/10/main/recovery.conf
  </code></pre>
  
  <p>and get a </p>
  
  <pre><code>-bash: /var/lib/postgresql/10/main/recovery.conf: Permission denied
  </code></pre>
  
  <p>How come I can create the file but not put content on it?</p>
  
  <p>I already tried giving 775 and 777 permisions using chmod </p>
  
  <pre><code>chmod 775 recovery.conf
  </code></pre>
  
  <p>as the psotgres user and i get  </p>
  
  <pre><code>-rwxrwxr-x 1 postgres postgres  133 May 11 22:11 recovery.conf
  </code></pre>
  
  <p>to this file as a postgres user and still the error persists so  im confused about whats going on</p>