title: How to quote parameters when calling a shell script from within another script using a variable
body: <p>I have two shell scripts. One script dynamically creates the call to the second script, based on the parameter it received, and then executes the call.</p>
  
  <p>My problem is that the parameters the first script gets may contain spaces, so I must quote the parameter in the call to script2.</p>
  
  <p>This is an example to the problem:</p>
  
  <p>script1.sh:</p>
  
  <pre><code>#!/bin/sh
  param=$1
  command="./script2.sh \"$param\""
  echo $command
  $command
  </code></pre>
  
  <p>script2.sh:</p>
  
  <pre><code>#!/bin/sh
  param=$1
  echo "the value of param is $param"
  </code></pre>
  
  <p>When I run:</p>
  
  <pre><code>./script1.sh "value with spaces"
  </code></pre>
  
  <p>I get:</p>
  
  <pre><code>./script2.sh "value with spaces"
  the value of param is "value
  </code></pre>
  
  <p>Which is of course not what I need.</p>
  
  <p>What is wrong here??</p>
  
  <p>TIA.</p>
  
  <p><strong>EDIT :</strong></p>
  
  <p>I found the solution thanks to the useful <a href="http://mywiki.wooledge.org/BashFAQ/050" rel="nofollow">link</a> in tripleee's comment. Here it is in case it helps anybody.</p>
  
  <p>In short, in order to solve this, one should use an array for the arguments.</p>
  
  <p>script1.sh:</p>
  
  <pre><code>#!/bin/sh
  param=$1
  
  args=("$param")
  script_name="./script2.sh"
  echo $script_name "${args[@]}"
  $script_name "${args[@]}"
  </code></pre>