title: Case...esac default condition
body: <p>I'm using <code>getopts</code> and <code>case...esac</code> for the first time. I've seen tutorials and questions with many different patterns for the default/catch-all option: <code>?</code>, <code>*</code>, <code>\?</code>, <code>[?]</code>. They all seem to function the same, as you can see if you change their order in this script:</p>
  
  <pre><code>#!/bin/bash
  set -e
  set -u
  set -o pipefail
  
  while getopts ":d:l:h:" opt; do
      case $opt in
          d)  echo getopts saw $opt with value $OPTARG ;;
          l)  echo getopts saw $opt with value $OPTARG ;;
          h)  echo getopts saw $opt with value $OPTARG ;;
          :)  echo "getopts saw $opt with value $OPTARG and went to :" ;;
          *)  echo "getopts saw $opt with value $OPTARG and went to *" ;;
          ?)  echo "getopts saw $opt with value $OPTARG and went to ?" ;;
          \?) echo "getopts saw $opt with value $OPTARG and went to \?" ;;
          [?]) echo "getopts saw $opt with value $OPTARG and went to [?]" ;;
      esac
  done
  echo script done
  </code></pre>
  
  <p>Running the code below will always stop at the first matching option in the <code>case</code> list:</p>
  
  <pre><code>$ ./test.sh -c
  getopts saw ? with value c and went to *
  script done
  </code></pre>
  
  <p>Are all of these exactly the same?</p>
  
  <p>In response to @BenjaminW.'s comment:
  The manual quote below explains that <code>getopts</code> will return <code>?</code> vs <code>:</code> depending on whether <code>getopts</code> is set to silent. So my modified questions are:</p>
  
  <ul>
  <li>Are <code>?</code>, <code>\?</code>, and <code>[?]</code>equivalent? </li>
  <li>Is the <code>*</code> unnecessary?</li>
  </ul>
  
  <p>From the manual:
  If a required argument is not found, and getopts is not silent, a question mark (‘?’) is placed in name, OPTARG is unset, and a diagnostic message is printed. If getopts is silent, then a colon (‘:’) is placed in name and OPTARG is set to the option character found.</p>