When you put double quotes around:
"$(virsh domblklist $vm --details | awk '/disk/{print $4}')"
You are preventing word splitting from occurring, which means that the contents of your array are a maximum of two elements (words).
If the array vms_disks
is initially empty, then "${vms_disks[@]}"
expands to nothing, so your array only has one element.
Assuming that the output of "$(virsh domblklist $vm --details | awk '/disk/{print $4}'
only has spaces between the elements, then I think you can just remove the quotes and allow word splitting to happen:
vms_disks=( "${vms_disks[@]}" $(virsh domblklist "$vm" --details | awk '/disk/{print $4}') )
Don't forget that quotes inside the command substitution (e.g. around $vm
) are still important!
Since you're already making the assumption that virsh domblklist $vm --details
output only contains newlines between records, a slightly more robust way to do the same thing would be to use a loop to build the array:
vms_disks=()
while IFS= read -r disk; do
vms_disks+=( "$disk" )
done < <(virsh domblklist "$vm" --details | awk '/disk/{print $4}')
A slightly shorter form to do the same thing would be to use mapfile
/readarray
:
virsh domblklist "$vm" --details | awk '/disk/{print $4}' |
mapfile -O ${#vms_disks[@]} -t vms_disks
I'm using -O ${#vms_disks[@]}
to start writing to the end of the array vms_disks
, in case it already exists and contains elements (this matches your attempt in the question).