Maybe it would work if you remove quotes around
git clone "$i"
So it would look like this:
git clone $i
I think that with quotes shell should treat all your line as one single argument instead of many:
git clone "-b branch1 --single-branch https://"$USER"@stash/repo1.git"
This looks wrong.
EDIT:
@CharlesDuffy pointed to another problem I missed: you should also have quotes around the entire line in the array definition (but this won't work if you really need internal quotes around $USER, see example below):
declare -a arr=(
"-b branch1 --single-branch https://$USER@stash/repo1.git"
https://"$USER"@stash/rep2.git
)
I checked this on my local machine, and it seems to work.
$ ls
one/ test.sh*
$ ( cd one/ ; git branch )
* another_branch
master
$ cat test.sh
#!/bin/bash
declare -a arr=(
"-b another_branch --single-branch one two"
)
for i in "${arr[@]}"
do
git clone $i
done
$ ls
one/ test.sh*
$ ./test.sh
Cloning into 'two'...
done.
$ ls
one/ test.sh* two/
$ ( cd two/ ; git branch )
* another_branch
$
EDIT2:
This will only work if you can safely omit internal quotes around $USER. If you need them you should use eval inside the for loop and also quote the internal quotes in array declaration.
$ cat ./test.sh
#!/bin/bash
USER="username containing spaces" # just for example!
git () {
echo "$5"
}
declare -a arr=(
"-b branch1 --single-branch https://\"$USER\"@stash/repo1.git"
)
for i in "${arr[@]}"
do
printf "without eval:\t"
git clone $i
printf "with eval:\t"
eval "git clone $i"
done
$ ./test.sh
without eval: https://"username
with eval: https://username containing spaces@stash/repo1.git
This is yet another mistake that @CharlesDuffy found in my answer. Thanks Charles, I learn a lot from digging deeper into the problems you pointed!
-b, the second element isbranch1, etc.