title: How to get the output of a spawned child_process in Node.JS?
body: <p>First of all, I'm a complete noob and started using Node.JS yesterday (it was also my first time using Linux in years) so please be nice and explicit</p>
  
  <p>I'm currently making a Node.JS program which has to, among other things, launch shell commands (mainly : mount an usb drive).
  I'm currently using </p>
  
  <pre><code>var spawn = require('child_process').spawnSync;
  
  function shspawn(command) {
      spawn('sh', ['-c', command], { stdio: 'inherit' });
  }
  
  shspawn('echo Hello world');
  shspawn('mkdir newdir');
  </code></pre>
  
  <p>etc. which is a really comfortable way to do it for me.
  The problem is that I'd like to store the output of, for example, a "ls" command in a variable, in a way like</p>
  
  <pre><code>var result = shspawn('ls -l')
  </code></pre>
  
  <p>I've read some examples online but they rarely use spawn and when they do, it doesn't work for me (I guess I may do something wrong, but again I'm a noob in Node)</p>
  
  <p>If you guys have a better idea than using child_process_spawnSync I'm open to any idea, but I'd like as long as possible to keep my program package-free :)</p>
  
  <p><strong>EDIT : I need it to work synchronously ! That's why I've started using spawnSync. I will be using some commands like dd, that takes time and needs to be fully finished before the program moves on to another command.</strong></p>