Artificial intelligent assistant

Unable to use a variable to specify the targets for rsync's "--exclude={..}" option within script My goal is to have my bash script run this command: rsync -azhi --dry-run --exclude={'file1.txt','file2.txt','*.sql'} /from-directory/ /to-directory/ ... when abstracted thusly: srcdir='/from-directory/' dstdir='/to-directory/' excludes="{'file1.txt','file2.txt','*.sql'}" rsync -azhi --dry-run --exclude="$excludes" "$srcdir" "$dstdir" `$srcdir` and `$dstdir` evaluate correctly, but `$excludes` is not being interpreted as I had hoped it would. From the command line, --exclude={'file1.txt','file2.txt','*.sql'} works great. But once I try to stuff that {...} string into a variable, it stops working.

Try using an array. For example:


excludes=(file1.txt file2.txt '*.sql')
rsync -azhi --dry-run $(printf -- "--exclude=%s " "${excludes[@]}") /from-directory/ /to-directory/


BTW, don't enclose `*.sql` in single-quotes if you **want** it to expand to all .sql files in the current dir. I'm guessing that's NOT what you want it to do, so I've quoted it.

That will expand to:


rsync -azhi --dry-run --exclude=file1.txt --exclude=file2.txt --exclude=*.sql /from-directory/ /to-directory/


alternatively:


rsync -azhi --dry-run --exclude="$(printf -- "%s," "${excludes[@]}" |
sed -e s/,$//)" /from-directory/ /to-directory/


will expand to:


rsync -azhi --dry-run --exclude=file1.txt,file2.txt,*.sql /from-directory/ /to-directory/

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy f41adc9c3bd10b00a48cf202299b54dd