title: How can I execute a group of commands as another user in Bash?
body: <p>There are already <a href="https://stackoverflow.com/questions/10220019/how-to-write-a-shell-script-that-runs-some-commands-as-superuser-and-some-comman">some existing questions</a> asked here about  running commands as another user. However, the question and answers focus on <em>a single command</em> instead of a long group of commands.</p>
  <p>For example, consider the following script:</p>
  <pre><code>#!/bin/bash
  set -e
  
  root_command -p param1  # run as root
  
  # these commands must be run as another user
  command1 -p 'parameter with &quot;quotes&quot; inline'
  command2 -p 'parameter with &quot;quotes&quot; inline'
  command3 -p 'parameter with &quot;quotes&quot; inline'
  </code></pre>
  <p>There are a couple of important points to note here:</p>
  <ul>
  <li><p>The final three commands must be run as another user using <code>su</code> or <code>sudo</code>. In the example there were three commands, but suppose that there were many more...</p>
  </li>
  <li><p>The commands themselves make use of single and double quotes.</p>
  </li>
  </ul>
  <p>The second point above prevents the use of the following syntax:</p>
  <pre><code>su somebody -c &quot;command&quot;
  </code></pre>
  <p>...since the commands themselves contain quotes.</p>
  <p>What is the proper way to &quot;group&quot; the commands and run them under another user account?</p>