Search code examples
bashrsyncquotes

bash: rsync escape single quote


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 '[email protected]:/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""'"

Solution

  • 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.