Escaping from sudo within a script
<p>I have a script that calls another script as sudo. From within the second script, I want to be able to run another process <em>without</em> sudo permissions (the process in question is <code>brew</code>, which doesn't play nicely with <code>sudo</code>).</p>

<p>Here's the code in question:</p>

<p>scriptA.sh:</p>

<pre><code>#!/bin/sh

sudo ./scriptB.sh
</code></pre>

<p>scriptB.sh:</p>

<pre><code>#!/bin/sh

# This runs as sudo, but I need it to run as a regular user e.g. `username`
brew update
</code></pre>

<p>I tried <code>su -c "brew update" -s /bin/sh username</code>, but OS X's <code>su</code> doesn't allow the <code>c</code> flag.</p>