title: Setting a shell variable from stdout *or* fallback if command fails
body: <p>My script calls an external command (e.g. <code>readlink</code>) which will either:</p>
  
  <ul>
  <li>write a useful string to its stdout on success, or…</li>
  <li>return a non-zero exit status on failure</li>
  </ul>
  
  <p>I know how to do each individually, e.g. to capture the stdout I can use</p>
  
  <pre><code>MY_VAR=$(readlink /)
  </code></pre>
  
  <p>Or to check the result I could use:</p>
  
  <pre><code>if readlink /tmp; then
     echo "Success"
  fi
  </code></pre>
  
  <p>But is there a clean/clear way to do both ± simultaneously? I'm not sure the following would work:</p>
  
  <pre><code>if MY_VAL=`readlink "$MY_ARG"`; then
      echo "Value is ${MY_VAL}"
  else
      echo "Not found"
  fi
  </code></pre>
  
  <p><strong>UPDATE</strong>: so I tried that, and it <em>did</em> work. But is it only a coincidence, e.g. happens to work because readlink's output is empty at the same time it errors?</p>
  
  <p>As a particular constraint, I need to do this in an environment where <code>set -e</code> is specified, i.e. if other commands return an error the overall script will fail. Generic shell preferred, unless bash has a better mechanism.</p>