Rsync parameter in variable

I want to put the rsync command parameter in a variable so that I can reuse it for other rsync commands. Here is what I tried, but it did not work.

roption="-a --recursive --progress --exclude='class' --delete --exclude='exclude' --exclude='.svn' --exclude='.metadata' --exclude='*.class'"
rsync "$roption" /media/CORSAIR/workspace ~/

Can someone help me deal with the problem?

Thank,

+3
source share
3 answers

Use shell arrays. They are extremely useful if you want to form strings using escape sequences and let them be literally printed. In addition, 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
+14
source

Since yours $roptionpresents several arguments, you should use $roption, not "$roption".

, . bash, :

roptions=(-a --recursive --progress --exclude='class' --delete --exclude='exclude' --exclude='.svn' --exclude='.metadata' --exclude='*.class')
rsync "${roptions[@]}" /media/CORSAIR/workspace ~
+1

You can try the “eval” command, which will ask the shell to parse the command line once before the eval gets its interpretation:

roption="-a --recursive --progress --exclude='class' --delete --exclude='exclude' --exclude='.svn' --exclude='.metadata' --exclude='*.class'" 

eval "rsync $roption /media/CORSAIR/workspace ~"
-2
source

Source: https://habr.com/ru/post/1764432/


All Articles