title: Bash: share variables between functions calls
body: <p>I have a script with some functions that is loaded in the beginning of other scrips to do the common work. To avoid reprocess, I'm trying to create some kinda <em>cache</em> with some script scope variables. So this is my problem:</p>
  
  <p>supposing I have this scripts:</p>
  
  <p><em>functions.sh</em></p>
  
  <pre><code>#!/bin/bash
  v_cache=
  
  set_cache()
  {
      v_cache=1
      echo 1
  }
  echo_cache()
  {
      echo "v_cache=$v_cache"
  }
  </code></pre>
  
  <p><em>test.sh</em></p>
  
  <pre><code>#!/bin/bash
  . function.sh
  
  var=`set_cache`
  echo_cache
  
  set_cache
  echo_cache
  </code></pre>
  
  <p>Here is the output:</p>
  
  <pre><code>$ ./test.sh 
  v_cache=
  1
  v_cache=1
  </code></pre>
  
  <p>Calling a function in an attribution (either with `<em>func</em>` or $(<em>func</em>)) have different context than a simple call. I need to keep the same scope  to all functions calls in the same script (<em>test.sh</em> for example).</p>
  
  <p>For the example above, the output I was expecting is:</p>
  
  <pre><code>$ ./test.sh 
  v_cache=1
  1
  v_cache=1
  </code></pre>
  
  <p>I tried to find some explanation for why it works this way, but can't found nothing. I think `` trigger a new <em>bash</em> execution, completely independent.</p>
  
  <p>There's a way to share a variable through all functions calls? What are the alternatives to bypass this behaviour?</p>