title: How do I pass on script arguments that contain quotes/spaces?
body: <p>I'm trying to write a script <code>notify-finish</code> that can be prepended to any command. When done, it will run the command given by the arguments following, then email the user when the command is complete. Here's what I have:</p>
  
  <pre><code>PROG=$1
  # Run command given by arguments
  $@
  ECODE=$?
  echo -e "Subject: `hostname`: $PROG finished\r\nTo: &lt;$USER&gt;\r\n\r\nExited with $ECODE\r\n" | sendmail $USER
  </code></pre>
  
  <p>This works most of the time, but when arguments contain spaces, the quoting is stripped off. </p>
  
  <p>Working example:</p>
  
  <pre><code>notify-finished rsync -avz source/ user@remote:dest/
  </code></pre>
  
  <p>Failing example:</p>
  
  <pre><code>notify-finished rsync -avz -e 'ssh -c blowfish' source/ user@remote:dest/
  </code></pre>
  
  <p>In the second case, <code>$@</code> is expanded out to <code>rsync -avz -e ssh -c blowfish source user@remote:dest/</code>, missing the single quotes. It does not work with double-quotes either, nor with <code>$*</code>.</p>
  
  <p>After reading other posts I tried putting the command in an array, but I get the exact same issue:</p>
  
  <pre><code>CMD=(notify-finished rsync -avz -e 'ssh -c blowfish' source/ user@remote:dest/)
  ${CMD[@]}
  </code></pre>
  
  <p>How do I make this work for all arguments?</p>