Getting bash script to update parent shell's Environment
<p>I am attempting to write a <code>bash</code> command line tool that is usable immediately after installation, i.e. in the same shell as its installation script was called. Lets say <code>install-script.sh</code> (designed for Ubuntu) looks like:</p>

<pre><code># Get the script's absolute path:
pushd `dirname $0` &gt; /dev/null
SCRIPTPATH=`pwd`
popd &gt; /dev/null

# Add lines to bash.bashrc to export the environment variable:
echo "SCRIPT_HOME=${SCRIPTPATH}" &gt;&gt; /etc/bash.bashrc
echo "export SCRIPT_HOME" &gt;&gt; /etc/bash.bashrc

# Create a new command:
cp ${SCRIPTPATH}/newcomm /usr/bin
chmod a+x /usr/bin/newcomm
</code></pre>

<p>The idea is that the new command <code>newcomm</code> uses the <code>SCRIPT_HOME</code> environment variable to reference the main script - which is also in <code>SCRIPTPATH</code>:</p>

<pre><code>exec "${SCRIPT_HOME}/main-script.sh"
</code></pre>

<p>Now, the updated <code>bash.bashrc</code> hasn't been loaded into the parent shell yet. Worse, I cannot <code>source</code> it from within the script - which is running in a child shell. Using <code>export</code> to change <code>SCRIPT_HOME</code> in the parent shell would at best be duct-taping the issue, but even this is impossible. Also note that the installation script needs to be run using <code>sudo</code> so it cannot be called from the parent shell using <code>source</code>.</p>

<p>It should be possible since package managers like <code>apt</code> do it. Is there a <em>robust</em> way to patch up my approach? How is this usually done, and is there a good guide to writing bash installers?</p>