If the number of elements is guaranteed to be a multiple of 4 and greater than 0:
$ myarray=(22 3 2 0 22 4 5 8 22 4 3 6)
$ printf '%s,%s,%s,%s\n' "${myarray[@]}"
22,3,2,0
22,4,5,8
22,4,3,6
If not, that gives:
$ myarray=(22 3 2 0 22 4 5 8 22 4 3 6 extra)
$ printf '%s,%s,%s,%s\n' "${myarray[@]}"
22,3,2,0
22,4,5,8
22,4,3,6
extra,,,
$ myarray=()
$ printf '%s,%s,%s,%s\n' "${myarray[@]}"
,,,
With zsh:
myarray=(22 3 2 0 22 4 5 8 22 4 3 6 extra)
while (( $#myarray )) {
print -r -- ${(j[,])myarray[1,4]}
myarray[1,4]=()
}
Which modifies $myarray or:
myarray=(22 3 2 0 22 4 5 8 22 4 3 6 extra)
for (( i = 1; i <= $#myarray; i += 4 ))
print -r -- ${(j[,])myarray[i,i+3]}
Which both give:
22,3,2,0
22,4,5,8
22,4,3,6
extra
That latter one you could do in bash with:
myarray=(22 3 2 0 22 4 5 8 22 4 3 6 extra)
(IFS=,
for (( i = 0; i < ${#myarray[@]}; i += 4 )) {
printf '%s\n' "${myarray[*]:i:4}"
}
)
Relying on "${array[*]}" to join the elements with the first character of $IFS as bash has no equivalent for zsh's join parameter expansion flag.
{0..$n..4}is not recognized as a brace expansion, and left as-is, and then after variable expansion it gives you{0..123..4}, so nothing you do inside the loop really matters.echo "${myarray[*]}"indicatesmyarrayis not an array but rather a single string; please update the question with the complete output fromtypeset -p myarray