title: Piping command output to tee but also save exit code of command
body: <p>I have a shell script in which I wrap a command (mvn clean install), to redirect the output  to a logfile. </p>
  
  <pre><code>#!/bin/bash
  ...
  mvn clean install $@ | tee $logfile
  echo $? # Does not show the return code of mvn clean install
  </code></pre>
  
  <p>Now if <code>mvn clean install</code> fails with an error, I want my wrapper shell script also fail with that error. But since I'm piping all the output to tee, I cannot access the return code of <code>mvn clean install</code>, so when I access <code>$?</code> afterwards, it's always 0 (since tee successes). </p>
  
  <p>I tried letting the command write the error output to a separate file and checking that afterwards, but the error output of mvn is always empty (seems like it only writes to stdout).</p>
  
  <p>How can I preserve the return code of <code>mvn clean install</code> but still piping the output to a logfile?</p>