title: Passing an array with spaces to a Bash function to act as its list of arguments
body: <p>I am trying to use <code>getopts</code> to make my script be able to take command line arguments, like <code>-s "gil.sh 123</code>. Because it does not support command line arguments with long names, I have a function that takes the arguments, and changes every appearance of the long version (in this case --script) to the short version (-s) and only then <code>getopts</code> gets called.</p>
  
  <p>The problem is that if it contains spaces (in this case "gil.sh 123") then I cannot get the second function to take this as an array with 2 members, in this case i get the array <code>(-s gil.sh 123)</code> instead of <code>(-s "gil.sh 123")</code> which is what I sent the function.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>#!/bin/bash
  #change long format arguments (-- and then a long name) to short format (- and then a single letter) and puts result in $parsed_args
  function parse_args()
  {
      m_parsed_args=("$@")
      #changes long format arguments (--looong) to short format (-l) by doing this:
      #res=${res/--looong/-l}
      for ((i = 0; i &lt; $#; i++)); do
          m_parsed_args[i]=${m_parsed_args[i]/--script/-s}
      done
  }
  
  #extracts arguments into the script's variables
  function handle_args()
  {
      echo "in handle_args()"
      echo $1
      echo $2
      echo $3
      while getopts ":hno:dt:r:RT:c:s:" opt; do
          case $opt in
              s)
                  #user script to run at the end
                  m_user_script=$OPTARG
                  ;;
              \?)
                  print_error "Invalid option: -$OPTARG"
                  print_error "For a list of options run the script with -h"
                  exit 1
                  ;;
              :)
                  print_error "Option -$OPTARG requires an argument."
                  exit 1
                  ;;
          esac
      done
  }
  
  parse_args "$@"
  handle_args ${m_parsed_args[@]}
  </code></pre>
  
  <p>(This code is obviously shorter than the original which has many more substitutions and types of arguments, i only left one)</p>
  
  <p>I call the script like this: <code>./tmp.sh -s "gil.sh 123"</code> and I can see that after <code>parse_args</code> the variable <code>m_parsed_args</code> is an array with 2 members, but when I send it to <code>handle_args</code> the array has 3 members, so I cannot give the variable <code>m_user_script</code> the correct value that i want it to get ("gil.sh 123")</p>