title: Grep used on SSH command output does not return a PIPESTATUS
body: <p>Grep doesn't return a pipestatus in the script but does in the shell.</p>
  
  <p>See the included script:</p>
  
  <pre><code>#!/usr/bin/env bash
  
  function testSshConnection() {
    local SSH_CMD="ssh -o BatchMode=yes -o ConnectTimeout=3 $1@$2 2&gt;&amp;1 | grep \"Host key verification failed.\""
    echo "RUNNING: $SSH_CMD"
    $SSH_CMD
    PIPE_RESULT="${PIPESTATUS[0]} ${PIPESTATUS[1]}"
    echo "FUNCTION: PIPE_RESULT: ${PIPE_RESULT}"
    PIPE_RESULT_ARRAY=($PIPE_RESULT)
    PIPE0="${PIPE_RESULT_ARRAY[0]}"
    PIPE1="${PIPE_RESULT_ARRAY[1]}"
    local RC=${PIPE1:-$PIPE0}
    echo "Returning RC=$RC"
    return $RC
  }
  
  while ! testSshConnection root $1; do
    PIPE_RESULT="${PIPESTATUS[0]} ${PIPESTATUS[1]}"
    echo "WHILE: PIPE_RESULT: ${PIPE_RESULT}"  
    PIPE_RESULT_ARRAY=($PIPE_RESULT)
    PIPE0="${PIPE_RESULT_ARRAY[0]}"
    PIPE1="${PIPE_RESULT_ARRAY[1]}"
    WAIT=4
    echo "Waiting $SLEEP seconds for host $1 to be accessible on port 22"
    sleep $WAIT
  done
  
  exit
  </code></pre>
  
  <p>Script Output:</p>
  
  <pre><code>~ $ /tmp/test.sh 46.101.7.220
  RUNNING: ssh -o BatchMode=yes -o ConnectTimeout=3 root@46.101.7.220 2&gt;&amp;1 | grep "Host key verification failed."
  Host key verification failed.
  FUNCTION: PIPE_RESULT: 255
  Returning RC=255
  WHILE: PIPE_RESULT: 255
   Waiting 4 seconds for host 46.101.7.220 to be accessible on port 22
  RUNNING: ssh -o BatchMode=yes -o ConnectTimeout=3 root@46.101.7.220 2&gt;&amp;1 | grep "Host key verification failed."
  Host key verification failed.
  FUNCTION: PIPE_RESULT: 255
  Returning RC=255
  WHILE: PIPE_RESULT: 255
  ...etc
  </code></pre>
  
  <p>Output from running the ssh command in a seperate shell:</p>
  
  <pre><code>~ $ ssh -o BatchMode=yes -o ConnectTimeout=3 root@46.101.7.220 2&gt;&amp;1 | grep "Host key verification failed."
  Host key verification failed.
  ~ $ echo "${PIPESTATUS[0]} ${PIPESTATUS[1]}"
  255 0
  </code></pre>
  
  <p>Why does the ssh command in the script not return a ${PIPESTATUS[1]} for the grep command?</p>