I want to put command option of rsync into a variable so I can reuse it for other rsync commands. Here is what I tried but it didn't work.
roption="-a --recursive --progress --exclude='class' --delete --exclude='exclude' --exclude='.svn' --exclude='.metadata' --exclude='*.class'"
rsync "$roption" /media/CORSAIR/workspace ~/
Can any body help me figure out the problem?
Thanks,
Use shell arrays. They're extremely useful if you want to form strings using escapes and have them be literally what is typed. Plus, security.
roption=(
-a
--recursive
--progress
--exclude='class'
--delete
--exclude='exclude'
--exclude='.svn'
--exclude='.metadata'
--exclude='*.class'
)
rsync "${roption[@]}" /media/CORSAIR/workspace ~/
You can even add to them:
if [ "$VERBOSE" -ne 0 ]; then
roption+=(--verbose)
fi
Since your $roption
represents more than one argument, you should use $roption
, not "$roption"
.
Of course, using a scalar to hold multiple values is just wrong. If you are using bash, consider using an array instead:
roptions=(-a --recursive --progress --exclude='class' --delete --exclude='exclude' --exclude='.svn' --exclude='.metadata' --exclude='*.class')
rsync "${roptions[@]}" /media/CORSAIR/workspace ~
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With