title: Create a persistent bash shell session in Node.js, know when commands finish, and read and modify sourced/exported variables
body: <p>Imagine this contrived scenario:</p>
  <p><em>./main.sh</em></p>
  <pre class="lang-sh prettyprint-override"><code>source ./config.sh
  SOME_CONFIG=&quot;${SOME_CONFIG}bar&quot;
  ./output.sh
  </code></pre>
  <p><em>./config.sh</em></p>
  <pre class="lang-sh prettyprint-override"><code>export SOME_CONFIG='foo'
  </code></pre>
  <p><em>./output.sh</em></p>
  <pre class="lang-sh prettyprint-override"><code>echo &quot;Config is: ${SOME_CONFIG}&quot;
  </code></pre>
  <hr />
  <p>I am trying to replace <em>./main.sh</em> with a Node.js powered <em>./main.js</em> WITHOUT replacing the other shell files. The exported <em>./config.sh</em> functions/variables must also be fully available to <em>./output.sh</em></p>
  <p>Here is a NON working ./main.js. I have written this for the sole purpose to explain what I want the final code to look like:</p>
  <pre class="lang-js prettyprint-override"><code>const terminal = require('child_process').spawn('bash')
  
  terminal.stdin.write('source ./config.sh\n')
  process.env.SOME_CONFIG = `${process.env.SOME_CONFIG}bar` // this must be done in JS
  terminal.stdin.write('./output.sh\n') // this must be able to access all exported functions/variables in config.sh, including the JS modified SOME_CONFIG
  </code></pre>
  <p>How can I achieve this? Ideally if there's a library that can do this I'd prefer that.</p>