title: Pass multi-word arguments to a bash function
body: <p>Inside a bash script function, I need to work with the command-line arguments of the script, and also with another list of arguments. So I'm trying to pass two argument lists to a function, the problem is that multi-word arguments get split.</p>
  
  <pre><code>function params()
  {
      for PARAM in $1; do
          echo "$PARAM"
      done
  
      echo .
  
      for ITEM in $2; do
          echo "$ITEM"
      done
  }
  
  PARAMS="$@"
  ITEMS="x y 'z t'"
  params "$PARAMS" "$ITEMS"
  </code></pre>
  
  <p>calling the script gives me</p>
  
  <pre><code>myscript.sh a b 'c d'
  a
  b
  c
  d
  .
  x
  y
  'z
  t'
  </code></pre>
  
  <p>Since there are two lists they must be passed as a whole to the function, the question is, how to iterate the elements while respecting multi-word items enclosed in single quotes 'c d' and 'z t'?</p>
  
  <p>The workaround that I have (see below) makes use of BASH_ARGV so I need to pass just a single list to the function. However I would like to get a better understanding of what's going on and what's needed to make the above work.</p>
  
  <pre><code>function params()
  {
      for PARAM in "${BASH_ARGV[@]}"; do
          echo "$PARAM"
      done
  
      echo .
  
      for ITEM in "$@"; do
          echo "$ITEM"
      done
  }
  
  params x y 'z t'
  </code></pre>
  
  <p>calling the script gives me</p>
  
  <pre><code>myscript.sh a b 'c d'
  c d
  b
  a
  .
  x
  y
  z t
  </code></pre>
  
  <p>... Which is how I need it (except that first list is reversed, but that would be tolerable I guess)</p>