title: Error handling in Bash
body: <p>What is your favorite method to handle errors in Bash? 
  The best example of handling errors I have found on the web was written by William Shotts, Jr at <a href="http://www.linuxcommand.org" rel="noreferrer">http://www.linuxcommand.org</a>. </p>
  
  <p>He suggests using the following function for error handling in Bash:</p>
  
  <pre><code>#!/bin/bash
  
  # A slicker error handling routine
  
  # I put a variable in my scripts named PROGNAME which
  # holds the name of the program being run.  You can get this
  # value from the first item on the command line ($0).
  
  # Reference: This was copied from &lt;http://www.linuxcommand.org/wss0150.php&gt;
  
  PROGNAME=$(basename $0)
  
  function error_exit
  {
  
  #   ----------------------------------------------------------------
  #   Function for exit due to fatal program error
  #       Accepts 1 argument:
  #           string containing descriptive error message
  #   ---------------------------------------------------------------- 
  
      echo "${PROGNAME}: ${1:-"Unknown Error"}" 1&gt;&amp;2
      exit 1
  }
  
  # Example call of the error_exit function.  Note the inclusion
  # of the LINENO environment variable.  It contains the current
  # line number.
  
  echo "Example of error with line number and message"
  error_exit "$LINENO: An error has occurred."
  </code></pre>
  
  <p>Do you have a better error handling routine that you use in Bash scripts?</p>