title: How to handle out of memory gracefully in shell scripts
body: <p>Is there a way to gracefully handle out-of-memory conditions in a shell script?</p>
  
  <pre class="lang-sh prettyprint-override"><code>$ cat test.sh
  #!/bin/sh
  i=asdf
  while true; do
    i="$i $i"
  done
  $ bash test.sh
  test.sh: xrealloc: cannot allocate 18446744072098939008 bytes
  </code></pre>
  
  <p>Many programming languages allow handling out-of-memory exceptions using simple try-catch constructs. Is it possible to gracefully handle out-of-memory conditions in shell scripts / Bash as well? How?</p>
  
  <p>Would it be possible to either free temporary buffers and attempt to continue execution, or do some custom error handling (save state) and exit with error?</p>