title: Get exit code for eval command in sh
body: <p>I'm trying to create a function in a shell script that takes a command and executes it using eval, and then does some post-processing based on the success of the command. Unfortunately the code is not behaving as I would expect. Here's what I have:</p>
  
  <pre><code>#!/bin/sh
  
  ...
  
  function run_cmd()
  {
          # $1 = build cmd
  
          typeset cmd="$1"
          typeset ret_code
  
          eval $cmd
          ret_code=$?
  
          if [ $ret_code == 0 ]
          then
                  # Process Success
          else
                  # Process Failure
          fi
  
  }
  
  run_cmd "xcodebuild -target \"blah\" -configuration Debug"
  </code></pre>
  
  <p>When the command (<code>$cmd</code>) succeeds, it works fine. When the command fails ( compilation error, for instance ), the script automatically exits before I can process the failure. Is there a way I can prevent eval from exiting, or is there a different approach I can take that will allow me achieve my desired behavior?</p>