title: bash script not returning return code to calling c++ program when using popen()
body: <p>I have a python script that gets called in a bash script like this:</p>
  
  <pre><code>python3 CheckRef.py --reference $EXPECTED --result $RESULT.npy
  echo "return code = $?"
  </code></pre>
  
  <p>The script prints the correct return code from the python script. For example it prints <code>254</code> if the python script returns <code>-2</code>.</p>
  
  <p>However, when I call the bash script in a c++ program the return code is empty. It should be returning <code>-2</code>.</p>
  
  <pre><code>testClass test;
  auto result = test.execute(command.c_str());
  std::cout &lt;&lt; "result is " &lt;&lt; result &lt;&lt; std::endl;
  </code></pre>
  
  <p>Could anybody advise what I'm doing wrong?</p>
  
  <p>This is the function <code>execute()</code></p>
  
  <pre><code>std::string testClass::execute(const char* cmd) {
          char buffer[128];
          std::string result = "";
          FILE* pipe = popen(cmd, "r");
          if (!pipe) throw std::runtime_error("popen() failed!");
          try {
              while (!feof(pipe)) {
                  if (fgets(buffer, 128, pipe) != NULL)
                      result += buffer;
              }
          } catch (...) {
              pclose(pipe);
              throw;
          }
          pclose(pipe);
          return result;
      }
  </code></pre>