1

I do file transfer using rsync from a server where the files/directories have space in their name. Using single quote, I escape the space and this works

rsync -svaz --progress 'root@192.168.1.2:/folder with space' '/downloads'

I am trying to write a bash script for the same but with no success, according to this thread, one can escape single quote by place it under a double quote. The following looks good

#!/bin/bash    
read -e -p "source path: " SOURCEPATH
read -e -p "destination path: " DESPATH
echo "rsync -svaz --progress" "'""$SOURCEPATH""'" "'""$DESPATH""'"

But it doesn't work

#!/bin/bash 
read -e -p "source path: " SOURCEPATH
read -e -p "destination path: " DESPATH
rsync -svaz --progress "'""$SOURCEPATH""'" "'""$DESPATH""'"
Community
  • 1
  • 1
kuruvi
  • 641
  • 1
  • 11
  • 28
  • 1
    The single quotes on that original command line are just quoting the spaces. The double quotes around the variable expansions are doing the same thing. You don't need both. Just forget the single quotes. (Also to get what you were trying you just needed `"'$SOURCEPATH'"` not `"'""$SOURCEPATH""'"` i.e. no need to drop in and out of double quotes like that.) – Etan Reisner Oct 07 '15 at 15:37

2 Answers2

1

For escaping space in bash you need to use \ its a backslash followed by a space.

If you are reading the input from other source then simply putting a quote (either single or double) will work.

rsync -svaz --progress "$SOURCEPATH" "$DESPATH"

Note: If you are already using escape character for space while reading your input then you must use double quotes.

akm
  • 830
  • 6
  • 20
0

You're overquoting. Simply quoting the parameter expansion ensures that the exact value, spaces and all, are passed as a single argument to rsync:

rsync -svaz --progress "$SOURCEPATH" "$DESPATH"

The single quotes in your original example aren't passed to rsync; they are removed by the shell before passing the enclosed string as an argument to rsync.

chepner
  • 497,756
  • 71
  • 530
  • 681