How to handle space in filename in bash script?

Viewed 1502

I want to automate rsyncing, and tried the following code:

TARGET="/media/USB\ DISK/Backup/"
rsync -av --delete ~/Data ${TARGET}

but the execution results in the following error:

rsync: link_stat "/media/USB\" failed: No such file or directory (2)
rsync: mkdir "/home/DISK/BACKUP" failed: No such file or directory (2)

because of the 'space' in the target filename. However, when I echo the command I can use that to run on the shell directly.

How to do it correctly (which brackets, with backslash or without?) and why?

3 Answers
Related