The following snippet of code is part of a larger Bash script I'm working on. My intention is to pass input variables as arguments to Rsync.
I managed to make most of it work, except for the "--exclude" parameter's arguments:
INCLUDE="/.vim*,/.git*,/.ssh,.bash_aliases,.bashrc"
rsync ${DRYRUNARG} -avzhP --delete ${SOURCE_DIRS} \
--include=${INCLUDE} \
--exclude={/VirtualBox_VMs/*/Snapshots,/snap,/Downloads,/.*} \
${MNTPNT}${DEST_DIR}
This code works perfectly for what I need, but I'd still like to see those directories that have to be excluded defined by a variable.
The resulting code that I need should be something like this:
INCLUDE="/.vim*,/.git*,/.ssh,.bash_aliases,.bashrc"
EXCLUDE="/VirtualBox_VMs/*/Snapshots,/snap,/Downloads,/.*"
rsync ${DRYRUNARG} -avzhP --delete ${SOURCE_DIRS} \
--include=${INCLUDE} \
--exclude=${EXCLUDE} \
${MNTPNT}${DEST_DIR}
The code above doesn't work. The command executes without error but fails to exclude anything. The funny part is that it does work if I define the EXCLUDE variable with just one directory, even though it follows the same structure used for the INCLUDE variable.
I'm sure it's some silly mistake I'm making. Where could it be?
--exclude
option a comma-separated list of directories. If you want to exclude multiple directories you need to either use a pattern that matches all the directories or (more likely) use more than one--exclude
option. As an aside, if you addset -x
at the top of your script you'll see the command Bash is trying to execute (useful for debugging issues like these).--exclude
is subject to brace expansion before the resulting patterns are matched. But brace expansion is the first thing to happen when the code is parsed (before variable substitution, in particular), it won't happen on the contents of variables.declare -a EXCLUDES={/VirtualBox_VMs/*/Snapshots,/snap,/Downloads,/.*})
and then generate the string from that.