Bash – rsync unknown option from bash script execution

bashlubuntushell-scriptUbuntu

I'm trying to simply sync a folder using rsync over my local network from my computer in front of me to the destination computer.

#!/bin/bash

echo "This script will sync from my Macbook Dropbox/scripts/ folder to ruth@10.0.0.9 @ Norms house"

OPTIONS="--recursive --ignore-existing --progress"
SRC_DIR="~/Dropbox/scripts/"
DST_DIR="ruth@10.0.0.9:~/scripts/"
rsync "$OPTIONS" "$SRC_DIR" "$DST_DIR"

To give myself write privileges

chmod +x nameofscript.sh

When I run it, it outputs:

rsync: --recursive --ignore-existing --progress: unknown option

How do I properly store these options and run it as a script?

Best Answer

By quoting "$OPTIONS", the shell is passing it to rsync as a single string, so rsync is trying to find a single option named "--recursive --ignore-existing --progress", which obviously doesn't exist, since these are three separate options.

This should fix it for you:

rsync $OPTIONS "$SRC_DIR" "$DST_DIR"

A better option might be to use a bash array to store your options.

OPTIONS=(
    --recursive
    --ignore-existing
    --progress
)
# ...
rsync "${OPTIONS[@]}" "$SRC_DIR" "$DST_DIR"

The advantage of using an array is that then you are able to introduce items that include spaces, if any are necessary.

Related Question