title: BASH getopts optional arguments
body: <p>I'm trying to create a shell script that has two mandatory arguments (arg1, arg2) and then an optional -b flag plus the argument that needs to follow should the user choose to use the flag.</p>
  
  <p>Let me explain:</p>
  
  <p>It's an install script that takes an advantage of GIT to fetch an application from a Github repository. The user types in terminal f.ex.:</p>
  
  <pre><code>./shellscript.sh new &lt;app_name&gt; # fetches master branch by default
  </code></pre>
  
  <p>and the script fetches an instance of the master branch from this repository. Should the user however choose to use the optional -b flag that would mean that he/she wants to specify which branch to fetch, e.g. develop branch. Meaning that the user could do:</p>
  
  <pre><code>./shellscript.sh new &lt;app_name&gt; -b develop # or which ever branch there is available
  </code></pre>
  
  <p>I'm also curious how you could go about making the script work so that it wouldn't matter if the user types in the -b flag+branch_name before the 'new' argument and the 'app_name' argument. But that is perhaps not the most important thing at the moment.</p>
  
  <p>To know what exactly I'm trying to build, here is a link to my current script that only takes the two mandatory arguments and only fetches the master branch: <a href="https://github.com/villimagg/The-Minty-Developer/blob/master/bin/artisan" rel="nofollow">My Super Cool Artisan Executable Script</a></p>
  
  <p>P.S.: I've been trying out many examples using getopts which I've found both here on Stackoverflow and other blogs out there but none have helped me to completely make it work. Thus am I here asking y'all great people for help.</p>
  
  <p>Huge thanks and be sure to check out my <a href="https://github.com/villimagg/The-Minty-Developer" rel="nofollow">Linux Mint / Ubuntu - Post Install Script for only cool people(you guys and those switching over to Linux from Windows/Mac)</a></p>
  
  <p>Regards,
  Villi</p>