Search code examples
rubyshellstdoutexit-code

How can an external command return both its output and its exit status to Ruby?


This is a follow up question regarding ruby system command check exit code. I want to run command such that to get its output as well as exit code. Currently what I used in the code is:

rv = `#{cmd} 2>&1`

But this only captures output, and

rv = system(cmd)

only captures the exit code. How to achieve both?


Solution

  • Check $?.exitstatus for the exit code.

    For more info, see http://www.ruby-doc.org/core-2.1.0/Process/Status.html