title: Restore previous value or unset environment variable
body: <p>I have something like that in a shell script</p>
  
  <pre><code>cd /some/folder1
  savedCFLAGS=${CFLAGS-}
  export CFLAGS="-fexceptions ${CFLAGS-}"
  ./configure --some-option -what-ever
  make
  export CFLAGS=${savedCFLAGS}
  
  cd /some/folder2
  ./configure --some-option -what-ever
  make
  </code></pre>
  
  <p>This works more or less correctly but has one serious flaw.<br>If CFLAGS was unset previously, after the first block it will be set and empty.<br>Sometimes, this is a problem, because some configure scripts use default values for variables only if they are actually unset - default value will not be used if the variable is simply empty.</p>
  
  <p>Let me also state that I have such constructs multiple times in the script, with multiple different variables(this is a script used to build complete GCC-based toolchain).</p>
  
  <p>What would be the easiest way to have the variable either restored to previous value (if it actually was set previously) or unset (if it was unset before my modifications)? <br>I could set <code>savedCFLAGS</code> to some special token if it was unset, and then test whether <code>savedCFLAGS</code> has a value equal to that token.<br>However, maybe there's something much simpler?</p>