title: Using a glob expression passed as a bash script argument
body: <h3>TL;DR:</h3>
  
  <p>Why isn't invoking <code>./myscript foo*</code> when <code>myscript</code> has <code>var=$1</code> the same as invoking <code>./myscript</code> with <code>var=foo*</code> hardcoded?</p>
  
  <hr>
  
  <h3>Longer form</h3>
  
  <p>I've come across a weird issue in a bash script I'm writing. I am sure there is a simple explanation, but I can't figure it out.</p>
  
  <p>I am trying to pass a command line argument to be assigned as a variable in the script.</p>
  
  <p>I want the script to allow 2 command line arguments as follows:</p>
  
  <pre><code>$ bash my_bash_script.bash args1 args2
  </code></pre>
  
  <p>In my script, I assigned variables like this:</p>
  
  <pre><code>ARGS1=$1
  ARGS2=$2
  </code></pre>
  
  <p>Args 1 is a string descriptor to add to the output file.</p>
  
  <p>Args 2 is a group of directories: "dir1, dir2, dir3", which I am passing as <code>dir*</code> </p>
  
  <p>When I assign <code>dir*</code> to ARGS2 in the script it works fine, but when I pass <code>dir*</code> as the second command line argument, it only includes <code>dir1</code> in the wildcard expansion of <code>dir*</code>.</p>
  
  <p>I assume this has something to do with how the shell handles wildcards (even when passed as args), but I don't really understand it.</p>
  
  <p>Any help would be appreciated.</p>
  
  <hr>
  
  <h3>Environment / Usage</h3>
  
  <p>I have a group of directories: </p>
  
  <pre><code>dir_1_y_map, dir_1_x_map, dir_2_y_map, dir_2_x_map,
      ... dir_10_y_map, dir_10_x_map...
  </code></pre>
  
  <p>Inside these directories I am trying to access a file with extension <code>".status"</code> via <code>*.status</code>, and <code>".report.txt"</code> via <code>*report.txt</code>.</p>
  
  <p>I want to pass <code>dir_*_map</code> as the second argument to the script and store it in the variable ARGS2, then use it to search within each of the directories for the <code>".status"</code> and <code>".report"</code> files.</p>
  
  <p>The issue is that passing <code>dir_*_map</code> from the command line doesn't give the list of directories, but rather just the first item in the list.  If I assign the variable <code>ARGS2=dir_*_map</code> within the script, it works as I intend.</p>
  
  <hr>
  
  <h3>Workaround: Quoting</h3>
  
  <p>It turns out that passing the second argument in quotes allowed the wildcard expansion to work appropriately for <code>"dir_*_map"</code></p>
  
  <pre><code>#!/usr/bin/env bash
  ARGS1=$1    
  ARGS2=$2
  
  touch $ARGS1".extension"
  
  for i in /$ARGS2/*.status
  do
      grep -e "string" $i &gt;&gt; $ARGS1".extension"
  done
  </code></pre>
  
  <p>Here is an example invocation of the script:</p>
  
  <pre><code>sh ~/path/to/script descriptor "dir_*_map"
  </code></pre>
  
  <p>I don't fully understand when/why some arguments must be passed in quotes, but I assume it has to do with the wildcard expansion in the for loop.</p>