I'm trying to pass a variable to exclude several folders from a rsync operation in a script, as follows:
echo "Type the path of the folders to exclude: "
while read folder
do
folders=("${folders[@]}""$folder",)
while
rsync -avh source/* destination --exclude={"${folders[@]}"}
My source folder has the following structure:
- file1.txt
- file2.txt
- dir1
- dir2
- file3.txt
- dir2
- flie4.txt
- dir3
When I run my script and type dir2, press , type dir3, press again and press + D to exit the while loop, rsync won't exclude the folders given, showing the result below:
$ ./script
Type the path to the folders to exclude:
dir2
dir3
sending incremental file list
created directory destination
file1.txt
file2.txt
dir1/
dir1/dir2/
dir1/dir2/file3.txt
dir2/
dir2/file4.txt
dir3/
if you run the script and echo the variable folders
as echo "${folders[@]}"
before calling rsync we get the value given dir2,dir3,
, but I don't know why rsync isn't expanding the variable. I also tried to pass just $folders
to the --exclude option, but although it still show the correct value, it won't get expanded inside the refered option.
The script will work only if I pass a value manually or pass the first value manually and just pass one folder inside the variable, as below:
folders="dir3"
rsync -avh source/ destination --exclude={dir2,"$folders"}
This makes me believe that everytime I put a comma inside the bash variable, rsync won't be able to recognize it as a comma or bash simply won't expand the variable.
Anyone knows what's happening? I don't have any clue.
I'm using ubuntu 20.04 Focal Fossa (Development Branch).