title: Return value of redirected bash command
body: <p>The following script tries to create a directory and it fails because temp1 doesn't exist. I want to redirect the output with "tee." The problem is that "$?" catches the return value of "tee" which succeeds and not the value of "mkdir" which fails. How can I use "tee" but check the return value of "mkdir"</p>
  
  <p>file name: ./test.sh</p>
  
  <pre><code>#!/bin/bash
  
  mkdir temp1/temp2 | tee test_output.txt
  if [ "$?" != 0 ]; then
      echo "Command failed"
  else
      echo "Command successed"
  fi
  </code></pre>
  
  <p>./test.sh</p>
  
  <p>mkdir: cannot create directory `temp1/temp2': No such file or directory</p>
  
  <p>Command successed</p>