title: Behavior of the 'return' statement in Bash functions
body: <p>I'm having trouble understanding the behavior of the <code>return</code> built-in in Bash. Here is a sample script.</p>
  <pre><code>#!/bin/bash
  
  dostuff() {
      date | while true; do
          echo returning 0
          return 0
          echo really-notreached
      done
  
      echo notreached
      return 3
  }
  
  dostuff
  echo returncode: $?
  </code></pre>
  <p>The output of this script is:</p>
  <pre><code>returning 0
  notreached
  returncode: 3
  </code></pre>
  <p>If, however, the <code>date | </code> is removed from line 4, the output is as I expected:</p>
  <pre><code>returning 0
  returncode: 0
  </code></pre>
  <p>It seems like the <code>return</code> statement as used above is acting the way I thought the <code>break</code> statement ought to behave, but only when the loop is on the right hand side of a pipe. Why is this the case? I couldn't find anything to explain this behavior in the <a href="https://linux.die.net/man/1/bash" rel="nofollow noreferrer">Bash man page</a> or online. The script acts the same way in Bash 4.1.5 and <a href="https://en.wikipedia.org/wiki/Almquist_shell#dash:_Ubuntu,_Debian_and_POSIX_compliance_of_Linux_distributions" rel="nofollow noreferrer">Dash</a> 0.5.5.</p>