title: rsync multiple directories with quoted paths
body: <p>I am trying to rsync multiple directories in a single call inside of a bash script and am running into trouble with the syntax for quoting the paths.</p>
  
  <p>Here is what I am trying:</p>
  
  <pre><code>backuppath='/path/to/backup/folders/'
  declare -a backupitems=('folder1' 'folder2')
  backupitems=(${backupitems[@]/#/\"$backuppath})
  backupitems=(${backupitems[@]/%/\"})
  backup=${backupitems[@]}
  
  rsync ${backup} /path/to/destination
  </code></pre>
  
  <p>I get a link_stat error saying no such file or directory for <code>"/path/to/current/directory"/path/to/backup/folders/folder1""</code> and then the same error for folder2.  So it seems like it is generating the quoted path like I want it to, but rsync is interpreting the paths as relative and adding on the path to the current directory at the front.  rsync works correctly if I do </p>
  
  <pre><code>backuppath='path/to/backup/folders/folder1'
  rsync "${backuppath}" /path/to/destination
  </code></pre>
  
  <p>putting the quotes into the rsync command, but I can't do this with multiple folders within one variable because it treats the multiple directories as a long single path.  I got the script to work using the second method by looping over the folders and calling rsync on each one, but this method is slightly more awkward because of the way other parts of the script handle the folders so I would like to get the first method to work if there is a quick fix.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>For the top version above, with no spaces in any of the directory names, I see the following command using <code>set -vx</code>: 
      rsync '"/path/to/backup/folders/folder1"' '"/path/to/backup/folders/folder2"' /path/to/destination</p>
  
  <p>and I get the following error message: </p>
  
  <pre><code>rsync: link_stat "/path/to/current/directory/"/path/to/backup/folders/folder1"" failed: No such file or directory (2)
  </code></pre>
  
  <p>If I use the version suggested by @kdubs, then everything works when there are no spaces in the paths.</p>
  
  <p>When there are spaces in the path, kdubs version results in the command:</p>
  
  <p>rsync /path/to/back up/folders/folder1 /path/to/back up/folders/folder2 /path/to/destination</p>
  
  <p>and the errors:</p>
  
  <pre><code>rsync: link_stat "/path/to/back" failed: No such file or directory (2)
  rsync: link_stat "/path/to/back up/up/folder1" failed: No such file or directory (2)
  </code></pre>
  
  <p>My first version needs an additional tweak to work with spaces because expanding the array with [@] and creating a new array by enclosing the expansion with () causes the spaces in the path to break up the path into multiple array elements (see Tonin's answer below).</p>