title: BASH: Global variables aren't updateable in a function only when that function is piped (simple example)
body: <p>This smells buggy, but probably, someone can explain it:</p>
  
  <p>The following script doesn't work, the output is below:</p>
  
  <pre><code>#!/bin/bash
  GLOBAL_VAR="OLD"
  myfunc() {
          echo "func before set&gt; $GLOBAL_VAR"
          GLOBAL_VAR="NEW"
          echo "func after set&gt; $GLOBAL_VAR"
  }
  myfunc | cat
  echo "final value&gt; $GLOBAL_VAR"
  </code></pre>
  
  <p><strong>Output:</strong></p>
  
  <pre><code>func before set&gt; OLD
  func after set&gt; NEW
  final value&gt; OLD
  </code></pre>
  
  <hr>
  
  <p><strong>Now, just take off the <code>| cat</code> and it works!</strong></p>
  
  <pre><code>#!/bin/bash
  GLOBAL_VAR="OLD"
  myfunc() {
          echo "func before set&gt; $GLOBAL_VAR"
          GLOBAL_VAR="NEW"
          echo "func after set&gt; $GLOBAL_VAR"
  }
  myfunc
  echo "final value&gt; $GLOBAL_VAR"
  </code></pre>
  
  <p><strong>Output:</strong></p>
  
  <pre><code>func before set&gt; OLD
  func after set&gt; NEW
  final value&gt; NEW
  </code></pre>