title: How can I make one bash subshell exit the main calling shell script?
body: <p>Having the following bash script:</p>
  
  <pre><code>#!/bin/bash
  
  set -e
  
  function foo() {
    # commands that might fails and I want to exit my script
    ...
    echo "result I need as output"
  }
  
  my_var=$(foo)
  
  echo "I don't want this if there is an error inside foo"
  </code></pre>
  
  <p>Using <code>set -e</code> (in bash <code>4.4.19</code>) does not seem to work with subshells i.e. the last <code>echo</code> command is still being executed). How can I write the code to make the script exit if any of the commands inside <code>foo</code> terminate with non-zero exit code.</p>
  
  <p>I am using bash <code>GNU bash, version 4.4.19(1)-release (x86_64-apple-darwin16.7.0)</code> and the result of calling my script is (where the dots are replaced with an invalid command <code>head -this</code>:</p>
  
  <pre><code>$ ./my_script
  head: illegal option -- t
  usage: head [-n lines | -c bytes] [file ...]
  I don't want this if there is an error inside foo
  </code></pre>