title: Reading $OPTARG for optional flags?
body: <p>I'd like to be able to accept both mandatory and optional flags in my script. Here's what I have so far.</p>
  <pre><code>#!bin/bash
  
  while getopts &quot;:a:b:cdef&quot; opt; do
        case $opt in
          a ) APPLE=&quot;$OPTARG&quot;;;
          b ) BANANA=&quot;$OPTARG&quot;;;
          c ) CHERRY=&quot;$OPTARG&quot;;;
          d ) DFRUIT=&quot;$OPTARG&quot;;;
          e ) EGGPLANT=&quot;$OPTARG&quot;;;
          f ) FIG=&quot;$OPTARG&quot;;;
          \?) echo &quot;Invalid option: -&quot;$OPTARG&quot;&quot; &gt;&amp;2
              exit 1;;
          : ) echo &quot;Option -&quot;$OPTARG&quot; requires an argument.&quot; &gt;&amp;2
              exit 1;;
        esac
      done
  echo &quot;Apple is &quot;$APPLE&quot;&quot;
  echo &quot;Banana is &quot;$BANANA&quot;&quot;
  echo &quot;Cherry is &quot;$CHERRY&quot;&quot;
  echo &quot;Dfruit is &quot;$DFRUIT&quot;&quot;
  echo &quot;Eggplant is &quot;$EGGPLANT&quot;&quot;
  echo &quot;Fig is &quot;$FIG&quot;&quot;
  </code></pre>
  <p>However, the output for the following:</p>
  <pre><code>bash script.sh -a apple -b banana -c cherry -d dfruit -e eggplant -f fig
  </code></pre>
  <p>...outputs this:</p>
  <pre><code>Apple is apple
  Banana is banana
  Cherry is 
  Dfruit is 
  Eggplant is 
  Fig is
  </code></pre>
  <p>As you can see, the optional flags are not pulling the arguments with $OPTARG as it does with the required flags. Is there a way to read $OPTARG on optional flags without getting rid of the neat &quot;:)&quot; error handling?</p>
  <h2>=======================================</h2>
  <p><strong>EDIT:</strong> I wound up following the advice of Gilbert below. Here's what I did:</p>
  <pre><code>#!/bin/bash
  
    if [[ &quot;$1&quot; =~ ^((-{1,2})([Hh]$|[Hh][Ee][Ll][Pp])|)$ ]]; then
      print_usage; exit 1
    else
      while [[ $# -gt 0 ]]; do
        opt=&quot;$1&quot;
        shift;
        current_arg=&quot;$1&quot;
        if [[ &quot;$current_arg&quot; =~ ^-{1,2}.* ]]; then
          echo &quot;WARNING: You may have left an argument blank. Double check your command.&quot; 
        fi
        case &quot;$opt&quot; in
          &quot;-a&quot;|&quot;--apple&quot;      ) APPLE=&quot;$1&quot;; shift;;
          &quot;-b&quot;|&quot;--banana&quot;     ) BANANA=&quot;$1&quot;; shift;;
          &quot;-c&quot;|&quot;--cherry&quot;     ) CHERRY=&quot;$1&quot;; shift;;
          &quot;-d&quot;|&quot;--dfruit&quot;     ) DFRUIT=&quot;$1&quot;; shift;;
          &quot;-e&quot;|&quot;--eggplant&quot;   ) EGGPLANT=&quot;$1&quot;; shift;;
          &quot;-f&quot;|&quot;--fig&quot;        ) FIG=&quot;$1&quot;; shift;;
          *                   ) echo &quot;ERROR: Invalid option: \&quot;&quot;$opt&quot;\&quot;&quot; &gt;&amp;2
                                exit 1;;
        esac
      done
    fi
  
    if [[ &quot;$APPLE&quot; == &quot;&quot; || &quot;$BANANA&quot; == &quot;&quot; ]]; then
      echo &quot;ERROR: Options -a and -b require arguments.&quot; &gt;&amp;2
      exit 1
    fi
  </code></pre>
  <p>Thanks so much, everyone. This works perfectly so far.</p>